annotate lwasm/lwasm.c @ 375:71f507f404f1

Add "testmode" pragma Add a pragma to allow testing more easily. Thanks to Erik G <erik@6809.org> for the patch.
author William Astle <lost@l-w.ca>
date Mon, 13 Jul 2015 20:35:16 -0600
parents 8e25147c2aa8
children 35d4213e6657
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1 /*
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
2 lwasm.c
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
3
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
4 Copyright © 2010 William Astle
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
5
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
6 This file is part of LWTOOLS.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
7
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
11 version.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
12
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
16 more details.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
17
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
20 */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
21
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
22 #include <stdio.h>
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
23 #include <stdarg.h>
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
24 #include <string.h>
2
7317fbe024af Clean up insane number of compiler warnings under -Wall
lost@l-w.ca
parents: 0
diff changeset
25 #include <ctype.h>
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
26
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
27 #include <lw_expr.h>
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
28 #include <lw_alloc.h>
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
29 #include <lw_string.h>
375
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
30 #include <lw_error.h>
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
31
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
32 #include "lwasm.h"
336
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
33 #include "instab.h"
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
34
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
35 int lwasm_expr_exportable(asmstate_t *as, lw_expr_t expr)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
36 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
37 return 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
38 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
39
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
40 int lwasm_expr_exportval(asmstate_t *as, lw_expr_t expr)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
41 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
42 return 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
43 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
44
249
1f1a28b797e1 Add trap for divide by zero in expression library
William Astle <lost@l-w.ca>
parents: 241
diff changeset
45 void lwasm_dividezero(void *priv)
1f1a28b797e1 Add trap for divide by zero in expression library
William Astle <lost@l-w.ca>
parents: 241
diff changeset
46 {
1f1a28b797e1 Add trap for divide by zero in expression library
William Astle <lost@l-w.ca>
parents: 241
diff changeset
47 asmstate_t *as = (asmstate_t *)priv;
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
48 lwasm_register_error(as, as -> cl, E_DIV0);
249
1f1a28b797e1 Add trap for divide by zero in expression library
William Astle <lost@l-w.ca>
parents: 241
diff changeset
49 }
1f1a28b797e1 Add trap for divide by zero in expression library
William Astle <lost@l-w.ca>
parents: 241
diff changeset
50
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
51 lw_expr_t lwasm_evaluate_var(char *var, void *priv)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
52 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
53 asmstate_t *as = (asmstate_t *)priv;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
54 lw_expr_t e;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
55 importlist_t *im;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
56 struct symtabe *s;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
57
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
58 s = lookup_symbol(as, as -> cl, var);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
59 if (s)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
60 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
61 e = lw_expr_build(lw_expr_type_special, lwasm_expr_syment, s);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
62 return e;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
63 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
64
210
5d969517db74 Added condundefzero pragma
William Astle <lost@l-w.ca>
parents: 209
diff changeset
65 if (as -> undefzero)
5d969517db74 Added condundefzero pragma
William Astle <lost@l-w.ca>
parents: 209
diff changeset
66 {
5d969517db74 Added condundefzero pragma
William Astle <lost@l-w.ca>
parents: 209
diff changeset
67 e = lw_expr_build(lw_expr_type_int, 0);
5d969517db74 Added condundefzero pragma
William Astle <lost@l-w.ca>
parents: 209
diff changeset
68 return e;
5d969517db74 Added condundefzero pragma
William Astle <lost@l-w.ca>
parents: 209
diff changeset
69 }
5d969517db74 Added condundefzero pragma
William Astle <lost@l-w.ca>
parents: 209
diff changeset
70
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
71 // undefined here is undefied unless output is object
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
72 if (as -> output_format != OUTPUT_OBJ)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
73 goto nomatch;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
74
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
75 // check for import
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
76 for (im = as -> importlist; im; im = im -> next)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
77 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
78 if (!strcmp(im -> symbol, var))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
79 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
80 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
81
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
82 // check for "undefined" to import automatically
70
ceab04fd2969 Fixed premature installation of external reference under UNDEFEXTERN pragma; should not resolve to external references until after the initial parsing pass
lost@l-w.ca
parents: 44
diff changeset
83 if ((as -> passno != 0) && !im && CURPRAGMA(as -> cl, PRAGMA_UNDEFEXTERN))
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
84 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
85 im = lw_alloc(sizeof(importlist_t));
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
86 im -> symbol = lw_strdup(var);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
87 im -> next = as -> importlist;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
88 as -> importlist = im;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
89 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
90
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
91 if (!im)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
92 goto nomatch;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
93
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
94 e = lw_expr_build(lw_expr_type_special, lwasm_expr_import, im);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
95 return e;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
96
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
97 nomatch:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
98 if (as -> badsymerr)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
99 {
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
100 lwasm_register_error2(as, as -> cl, E_SYMBOL_UNDEFINED, "%s", var);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
101 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
102 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
103 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
104
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
105 lw_expr_t lwasm_evaluate_special(int t, void *ptr, void *priv)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
106 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
107 switch (t)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
108 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
109 case lwasm_expr_secbase:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
110 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
111 // sectiontab_t *s = priv;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
112 asmstate_t *as = priv;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
113 if (as -> exportcheck && ptr == as -> csect)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
114 return lw_expr_build(lw_expr_type_int, 0);
158
8dead67ba607 Make constant sections always resolve with a constant base offset of zero instead of an undefined reference
lost@l-w.ca
parents: 142
diff changeset
115 if (((sectiontab_t *)ptr) -> flags & section_flag_constant)
8dead67ba607 Make constant sections always resolve with a constant base offset of zero instead of an undefined reference
lost@l-w.ca
parents: 142
diff changeset
116 return lw_expr_build(lw_expr_type_int, 0);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
117 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
118 }
142
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
119
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
120 case lwasm_expr_linedlen:
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
121 {
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
122 line_t *cl = ptr;
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
123 if (cl -> dlen == -1)
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
124 return NULL;
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
125 return lw_expr_build(lw_expr_type_int, cl -> dlen);
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
126 }
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
127 break;
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
128
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
129 case lwasm_expr_linelen:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
130 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
131 line_t *cl = ptr;
211
6f2e18f1fe67 Improve autobranchlength pragma
William Astle <lost@l-w.ca>
parents: 210
diff changeset
132 if (cl -> len != -1)
6f2e18f1fe67 Improve autobranchlength pragma
William Astle <lost@l-w.ca>
parents: 210
diff changeset
133 return lw_expr_build(lw_expr_type_int, cl -> len);
6f2e18f1fe67 Improve autobranchlength pragma
William Astle <lost@l-w.ca>
parents: 210
diff changeset
134
6f2e18f1fe67 Improve autobranchlength pragma
William Astle <lost@l-w.ca>
parents: 210
diff changeset
135 if (cl -> as -> pretendmax)
6f2e18f1fe67 Improve autobranchlength pragma
William Astle <lost@l-w.ca>
parents: 210
diff changeset
136 {
6f2e18f1fe67 Improve autobranchlength pragma
William Astle <lost@l-w.ca>
parents: 210
diff changeset
137 if (cl -> maxlen != 0)
6f2e18f1fe67 Improve autobranchlength pragma
William Astle <lost@l-w.ca>
parents: 210
diff changeset
138 {
240
41d64951eb32 Silence debug message
William Astle <lost@l-w.ca>
parents: 226
diff changeset
139 //fprintf(stderr, "Pretending max, len = %d\n", cl -> maxlen);
211
6f2e18f1fe67 Improve autobranchlength pragma
William Astle <lost@l-w.ca>
parents: 210
diff changeset
140 return lw_expr_build(lw_expr_type_int, cl -> maxlen);
6f2e18f1fe67 Improve autobranchlength pragma
William Astle <lost@l-w.ca>
parents: 210
diff changeset
141 }
6f2e18f1fe67 Improve autobranchlength pragma
William Astle <lost@l-w.ca>
parents: 210
diff changeset
142 }
6f2e18f1fe67 Improve autobranchlength pragma
William Astle <lost@l-w.ca>
parents: 210
diff changeset
143 return NULL;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
144 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
145 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
146
142
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
147 case lwasm_expr_linedaddr:
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
148 {
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
149 line_t *cl = ptr;
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
150 return lw_expr_copy(cl -> daddr);
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
151 }
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
152
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
153 case lwasm_expr_lineaddr:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
154 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
155 line_t *cl = ptr;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
156 if (cl -> addr)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
157 return lw_expr_copy(cl -> addr);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
158 else
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
159 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
160 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
161
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
162 case lwasm_expr_syment:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
163 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
164 struct symtabe *sym = ptr;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
165 return lw_expr_copy(sym -> value);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
166 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
167
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
168 case lwasm_expr_import:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
169 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
170 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
171 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
172
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
173 case lwasm_expr_nextbp:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
174 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
175 line_t *cl = ptr;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
176 for (cl = cl -> next; cl; cl = cl -> next)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
177 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
178 if (cl -> isbrpt)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
179 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
180 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
181 if (cl)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
182 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
183 return lw_expr_copy(cl -> addr);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
184 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
185 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
186 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
187
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
188 case lwasm_expr_prevbp:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
189 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
190 line_t *cl = ptr;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
191 for (cl = cl -> prev; cl; cl = cl -> prev)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
192 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
193 if (cl -> isbrpt)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
194 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
195 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
196 if (cl)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
197 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
198 return lw_expr_copy(cl -> addr);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
199 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
200 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
201 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
202 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
203 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
204 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
205
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
206 const char* lwasm_lookup_error(lwasm_errorcode_t error_code)
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
207 {
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
208 switch (error_code)
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
209 {
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
210 case E_6309_INVALID: return "Illegal use of 6309 instruction in 6809 mode";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
211 case E_6809_INVALID: return "Illegal use of 6809 instruction in 6309 mode";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
212 case E_ALIGNMENT_INVALID: return "Invalid alignment";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
213 case E_BITNUMBER_INVALID: return "Invalid bit number";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
214 case E_BITNUMBER_UNRESOLVED: return "Bit number must be fully resolved";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
215 case E_BYTE_OVERFLOW: return "Byte overflow";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
216 case E_CONDITION_P1: return "Conditions must be constant on pass 1";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
217 case E_DIRECTIVE_OS9_ONLY: return "Directive only valid for OS9 target";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
218 case E_DIV0: return "Division by zero";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
219 case E_EXEC_ADDRESS: return "Exec address not constant!";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
220 case E_EXPRESSION_BAD: return "Bad expression";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
221 case E_EXPRESSION_NOT_CONST: return "Expression must be constant";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
222 case E_EXPRESSION_NOT_RESOLVED: return "Expression not fully resolved";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
223 case E_FILE_OPEN: return "Cannot open file";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
224 case E_FILENAME_MISSING: return "Missing filename";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
225 case E_FILL_INVALID: return "Invalid fill length";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
226 case E_IMMEDIATE_INVALID: return "Immediate mode not allowed";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
227 case E_IMMEDIATE_UNRESOLVED: return "Immediate byte must be fully resolved";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
228 case E_INSTRUCTION_FAILED: return "Instruction failed to resolve.";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
229 case E_INSTRUCTION_SECTION: return "Instruction generating output outside of a section";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
230 case E_LINE_ADDRESS: return "Cannot resolve line address";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
231 case E_LINED_ADDRESS: return "Cannot resolve line data address";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
232 case E_OBJTARGET_ONLY: return "Only supported for object target";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
233 case E_OPCODE_BAD: return "Bad opcode";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
234 case E_OPERAND_BAD: return "Bad operand";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
235 case E_PADDING_BAD: return "Bad padding";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
236 case E_PRAGMA_UNRECOGNIZED: return "Unrecognized pragma string";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
237 case E_REGISTER_BAD: return "Bad register";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
238 case E_SETDP_INVALID: return "SETDP not permitted for object target";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
239 case E_SETDP_NOT_CONST: return "SETDP must be constant on pass 1";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
240 case E_STRING_BAD: return "Bad string condition";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
241 case E_SYMBOL_BAD: return "Bad symbol";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
242 case E_SYMBOL_MISSING: return "Missing symbol";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
243 case E_SYMBOL_UNDEFINED_EXPORT: return "Undefined exported symbol";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
244 case E_MACRO_DUPE: return "Duplicate macro definition";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
245 case E_MACRO_ENDM: return "ENDM without MACRO";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
246 case E_MACRO_NONAME: return "Missing macro name";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
247 case E_MACRO_RECURSE: return "Attempt to define a macro inside a macro";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
248 case E_MODULE_IN: return "Already in a module!";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
249 case E_MODULE_NOTIN: return "Not in a module!";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
250 case E_NEGATIVE_BLOCKSIZE: return "Negative block sizes make no sense!";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
251 case E_NEGATIVE_RESERVATION: return "Negative reservation sizes make no sense!";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
252 case E_NW_8: return "n,W cannot be 8 bit";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
253 case E_SECTION_END: return "ENDSECTION without SECTION";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
254 case E_SECTION_EXTDEP: return "EXTDEP must be within a section";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
255 case E_SECTION_FLAG: return "Unrecognized section flag";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
256 case E_SECTION_NAME: return "Need section name";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
257 case E_SECTION_TARGET: return "Cannot use sections unless using the object target";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
258 case E_STRUCT_DUPE: return "Duplicate structure definition";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
259 case E_STRUCT_NONAME: return "Cannot declare a structure without a symbol name.";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
260 case E_STRUCT_NOSYMBOL: return "Structure definition with no effect - no symbol";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
261 case E_STRUCT_RECURSE: return "Attempt to define a structure inside a structure";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
262 case E_SYMBOL_DUPE: return "Multiply defined symbol";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
263 case E_UNKNOWN_OPERATION: return "Unknown operation";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
264
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
265 case W_ENDSTRUCT_WITHOUT: return "ENDSTRUCT without STRUCT";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
266 case W_DUPLICATE_SECTION: return "Section flags can only be specified the first time; ignoring duplicate definition";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
267 case W_NOT_SUPPORTED: return "Not supported";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
268
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
269 default: return "Error";
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
270 }
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
271 }
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
272
375
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
273 /* keeping this as a separate error output for stability in unit test scripts */
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
274 void lwasm_error_testmode(line_t *cl, const char* msg, int fatal)
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
275 {
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
276 cl -> as -> testmode_errorcount++;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
277 fprintf(stderr, "line %d: %s : %s\n", cl->lineno, msg, cl->ltext);
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
278 if (fatal == 1) lw_error("aborting\n");
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
279 }
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
280
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
281 /* parse unit test input data from comment field */
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
282 void lwasm_parse_testmode_comment(line_t *l, lwasm_testflags_t *flags, lwasm_errorcode_t *err, int *len, char **buf)
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
283 {
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
284 *flags = 0;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
285
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
286 if (!l)
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
287 return;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
288
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
289 char* s = strstr(l -> ltext, ";.");
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
290 if (s == NULL) return;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
291
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
292 char* t = strstr(s, ":");
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
293 if (t == NULL)
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
294 {
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
295 /* parse: ;.8E0FCE (emitted code) */
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
296
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
297 if (buf == NULL) return;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
298
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
299 int i;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
300 *flags = TF_EMIT;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
301
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
302 s = s + 2; /* skip ;. prefix */
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
303 t = s;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
304 while (*t > 32) t++;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
305
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
306 if ((t - s) & 1)
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
307 {
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
308 lwasm_error_testmode(l, "bad test data (wrong length of hex chars)", 1);
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
309 return;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
310 }
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
311
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
312 *len = (t - s) / 2;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
313
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
314 t = lw_alloc(*len);
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
315 *buf = t;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
316
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
317 for (i = 0; i < *len; i++)
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
318 {
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
319 int val;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
320 sscanf(s, "%2x", &val);
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
321 *t++ = (char) val;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
322 s += 2;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
323 }
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
324 }
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
325 else
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
326 {
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
327 /* parse: ;.E:1000 or ;.E:7 (warnings or errors) */
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
328 *flags = TF_ERROR;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
329
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
330 char ch = toupper(*(t - 1));
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
331 if (ch != 'E') lwasm_error_testmode(l, "bad test data (expected E: flag)", 1);
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
332 sscanf(t + 1, "%d", (int*) err);
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
333 }
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
334 }
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
335
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
336 void lwasm_register_error_real(asmstate_t *as, line_t *l, lwasm_errorcode_t error_code, const char *msg)
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
337 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
338 lwasm_error_t *e;
209
52d9dd71f555 Make warning actually work.
William Astle <lost@l-w.ca>
parents: 200
diff changeset
339
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
340 if (!l)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
341 return;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
342
375
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
343 if (CURPRAGMA(l, PRAGMA_TESTMODE))
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
344 {
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
345 lwasm_testflags_t flags;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
346 lwasm_errorcode_t testmode_error_code;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
347 lwasm_parse_testmode_comment(l, &flags, &testmode_error_code, NULL, NULL);
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
348 if (flags == TF_ERROR)
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
349 {
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
350 l -> len = 0; /* null out bogus line */
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
351 l -> insn = -1;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
352 l -> err_testmode = error_code;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
353 if (testmode_error_code == error_code) return; /* expected error: ignore and keep assembling */
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
354
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
355 char buf[128];
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
356 sprintf(buf, "wrong error code (%d)", error_code);
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
357 lwasm_error_testmode(l, buf, 0);
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
358 return;
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
359 }
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
360 }
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
361
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
362 e = lw_alloc(sizeof(lwasm_error_t));
226
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 216
diff changeset
363
375
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
364 if (error_code >= 1000)
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
365 {
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
366 e->next = l->warn;
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
367 l->warn = e;
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
368 as->warningcount++;
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
369 }
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
370 else
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
371 {
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
372 e->next = l->err;
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
373 l->err = e;
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
374 as->errorcount++;
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
375 }
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
376
375
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
377 e -> code = error_code;
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
378 e -> charpos = -1;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
379
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
380 e -> mess = lw_strdup(msg);
226
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 216
diff changeset
381 }
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 216
diff changeset
382
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
383 void lwasm_register_error(asmstate_t *as, line_t *l, lwasm_errorcode_t err)
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
384 {
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
385 lwasm_register_error_real(as, l, err, lwasm_lookup_error(err));
226
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 216
diff changeset
386 }
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 216
diff changeset
387
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
388 void lwasm_register_error2(asmstate_t *as, line_t *l, lwasm_errorcode_t err, const char* fmt, ...)
226
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 216
diff changeset
389 {
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
390 char errbuff[1024];
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
391 char f[128];
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
392
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
393 sprintf(f, "%s %s", lwasm_lookup_error(err), fmt);
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
394
226
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 216
diff changeset
395 va_list args;
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
396
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
397 va_start(args, fmt);
226
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 216
diff changeset
398
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
399 (void) vsnprintf(errbuff, 1024, f, args);
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
400
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
401 lwasm_register_error_real(as, l, err, errbuff);
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
402
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
403 va_end(args);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
404 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
405
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
406 int lwasm_next_context(asmstate_t *as)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
407 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
408 int r;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
409 r = as -> nextcontext;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
410 as -> nextcontext++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
411 return r;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
412 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
413
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
414 void lwasm_emit(line_t *cl, int byte)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
415 {
13
c80e5a063967 Brought forward patch to fix segfault with output outside of a section
lost@l-w.ca
parents: 2
diff changeset
416 if (cl -> as -> output_format == OUTPUT_OBJ && cl -> csect == NULL)
c80e5a063967 Brought forward patch to fix segfault with output outside of a section
lost@l-w.ca
parents: 2
diff changeset
417 {
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
418 lwasm_register_error(cl -> as, cl, E_INSTRUCTION_SECTION);
13
c80e5a063967 Brought forward patch to fix segfault with output outside of a section
lost@l-w.ca
parents: 2
diff changeset
419 return;
c80e5a063967 Brought forward patch to fix segfault with output outside of a section
lost@l-w.ca
parents: 2
diff changeset
420 }
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
421 if (cl -> outputl < 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
422 cl -> outputl = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
423
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
424 if (cl -> outputl == cl -> outputbl)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
425 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
426 cl -> output = lw_realloc(cl -> output, cl -> outputbl + 8);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
427 cl -> outputbl += 8;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
428 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
429 cl -> output[cl -> outputl++] = byte & 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
430
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
431 if (cl -> inmod)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
432 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
433 asmstate_t *as = cl -> as;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
434 // update module CRC
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
435 // this is a direct transliteration from the nitros9 asm source
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
436 // to C; it can, no doubt, be optimized for 32 bit processing
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
437 byte &= 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
438
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
439 byte ^= (as -> crc)[0];
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
440 (as -> crc)[0] = (as -> crc)[1];
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
441 (as -> crc)[1] = (as -> crc)[2];
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
442 (as -> crc)[1] ^= (byte >> 7);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
443 (as -> crc)[2] = (byte << 1);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
444 (as -> crc)[1] ^= (byte >> 2);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
445 (as -> crc)[2] ^= (byte << 6);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
446 byte ^= (byte << 1);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
447 byte ^= (byte << 2);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
448 byte ^= (byte << 4);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
449 if (byte & 0x80)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
450 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
451 (as -> crc)[0] ^= 0x80;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
452 (as -> crc)[2] ^= 0x21;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
453 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
454 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
455 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
456
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
457 void lwasm_emitop(line_t *cl, int opc)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
458 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
459 if (opc > 0x100)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
460 lwasm_emit(cl, opc >> 8);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
461 lwasm_emit(cl, opc);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
462 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
463
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
464 lw_expr_t lwasm_parse_term(char **p, void *priv)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
465 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
466 asmstate_t *as = priv;
44
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
467 int neg = 1;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
468 int val;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
469
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
470 if (!**p)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
471 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
472
142
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
473 if (**p == '.'
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
474 && !((*p)[1] >= 'A' && (*p)[1] <= 'Z')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
475 && !((*p)[1] >= 'a' && (*p)[1] <= 'z')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
476 && !((*p)[1] >= '0' && (*p)[1] <= '9')
142
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
477 )
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
478 {
142
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
479 (*p)++;
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
480 return lw_expr_build(lw_expr_type_special, lwasm_expr_linedaddr, as -> cl);
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
481 }
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
482
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
483 if (**p == '*')
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
484 {
697bc543368c Implement distinction between . and * for OS9 modules
lost@l-w.ca
parents: 113
diff changeset
485 // special "symbol" for current line addr (*)
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
486 (*p)++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
487 return lw_expr_build(lw_expr_type_special, lwasm_expr_lineaddr, as -> cl);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
488 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
489
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
490 // branch points
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
491 if (**p == '<')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
492 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
493 (*p)++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
494 return lw_expr_build(lw_expr_type_special, lwasm_expr_prevbp, as -> cl);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
495 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
496 if (**p == '>')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
497 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
498 (*p)++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
499 return lw_expr_build(lw_expr_type_special, lwasm_expr_nextbp, as -> cl);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
500 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
501
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
502 // double ascii constant
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
503 if (**p == '"')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
504 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
505 int v;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
506 (*p)++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
507 if (!**p)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
508 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
509 if (!*((*p)+1))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
510 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
511 v = (unsigned char)**p << 8 | (unsigned char)*((*p)+1);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
512 (*p) += 2;
326
d399df78e1ab Allow trailing ' or " on ascii constants
Tom LeMense <tlemense@yahoo.com>
parents: 256
diff changeset
513
d399df78e1ab Allow trailing ' or " on ascii constants
Tom LeMense <tlemense@yahoo.com>
parents: 256
diff changeset
514 if (**p == '"')
d399df78e1ab Allow trailing ' or " on ascii constants
Tom LeMense <tlemense@yahoo.com>
parents: 256
diff changeset
515 (*p)++;
d399df78e1ab Allow trailing ' or " on ascii constants
Tom LeMense <tlemense@yahoo.com>
parents: 256
diff changeset
516
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
517 return lw_expr_build(lw_expr_type_int, v);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
518 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
519
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
520 if (**p == '\'')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
521 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
522 int v;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
523
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
524 (*p)++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
525 if (!**p)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
526 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
527
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
528 v = (unsigned char)**p;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
529 (*p)++;
326
d399df78e1ab Allow trailing ' or " on ascii constants
Tom LeMense <tlemense@yahoo.com>
parents: 256
diff changeset
530
d399df78e1ab Allow trailing ' or " on ascii constants
Tom LeMense <tlemense@yahoo.com>
parents: 256
diff changeset
531 if (**p == '\'')
d399df78e1ab Allow trailing ' or " on ascii constants
Tom LeMense <tlemense@yahoo.com>
parents: 256
diff changeset
532 (*p)++;
d399df78e1ab Allow trailing ' or " on ascii constants
Tom LeMense <tlemense@yahoo.com>
parents: 256
diff changeset
533
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
534 return lw_expr_build(lw_expr_type_int, v);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
535 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
536
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
537 if (**p == '&')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
538 {
113
7e621e00b887 Fixed uninitialized variable in &-prefix and %-prefix constant parsing
lost@l-w.ca
parents: 98
diff changeset
539 val = 0;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
540 // decimal constant
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
541 (*p)++;
44
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
542
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
543 if (**p == '-')
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
544 {
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
545 (*p)++;
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
546 neg = -1;
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
547 }
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
548
354
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
549 if (!**p || !strchr("0123456789", **p))
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
550 {
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
551 (*p)--;
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
552 if (neg < 0)
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
553 (*p)--;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
554 return NULL;
354
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
555 }
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
556
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
557 while (**p && strchr("0123456789", **p))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
558 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
559 val = val * 10 + (**p - '0');
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
560 (*p)++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
561 }
95
a23f732c5ce8 Fixed &-prefix decimal constant parse problem
lost@l-w.ca
parents: 94
diff changeset
562 return lw_expr_build(lw_expr_type_int, val * neg);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
563 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
564
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
565 if (**p == '%')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
566 {
113
7e621e00b887 Fixed uninitialized variable in &-prefix and %-prefix constant parsing
lost@l-w.ca
parents: 98
diff changeset
567 val = 0;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
568 // binary constant
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
569 (*p)++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
570
44
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
571 if (**p == '-')
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
572 {
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
573 (*p)++;
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
574 neg = -1;
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
575 }
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
576
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
577 if (**p != '0' && **p != '1')
354
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
578 {
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
579 (*p)--;
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
580 if (neg < 0)
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
581 (*p)--;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
582 return NULL;
354
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
583 }
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
584
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
585 while (**p && (**p == '0' || **p == '1'))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
586 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
587 val = val * 2 + (**p - '0');
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
588 (*p)++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
589 }
94
61c084b2c727 Fixed bug parsing %-prefix binary constants
lost@l-w.ca
parents: 70
diff changeset
590 return lw_expr_build(lw_expr_type_int, val * neg);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
591 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
592
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
593 if (**p == '$')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
594 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
595 // hexadecimal constant
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
596 int v = 0, v2;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
597 (*p)++;
44
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
598 if (**p == '-')
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
599 {
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
600 (*p)++;
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
601 neg = -1;
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
602 }
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
603
354
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
604 if (!**p || !strchr("0123456789abcdefABCDEF", **p))
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
605 {
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
606 (*p)--;
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
607 if (neg < 0)
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
608 (*p)--;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
609 return NULL;
354
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
610 }
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
611 while (**p && strchr("0123456789abcdefABCDEF", **p))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
612 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
613 v2 = toupper(**p) - '0';
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
614 if (v2 > 9)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
615 v2 -= 7;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
616 v = v * 16 + v2;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
617 (*p)++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
618 }
44
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
619 return lw_expr_build(lw_expr_type_int, v * neg);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
620 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
621
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
622 if (**p == '0' && (*((*p)+1) == 'x' || *((*p)+1) == 'X'))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
623 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
624 // hexadecimal constant, C style
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
625 int v = 0, v2;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
626 (*p)+=2;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
627
354
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
628 if (!**p || !strchr("0123456789abcdefABCDEF", **p))
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
629 {
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
630 (*p) -= 2;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
631 return NULL;
354
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
632 }
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
633 while (**p && strchr("0123456789abcdefABCDEF", **p))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
634 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
635 v2 = toupper(**p) - '0';
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
636 if (v2 > 9)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
637 v2 -= 7;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
638 v = v * 16 + v2;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
639 (*p)++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
640 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
641 return lw_expr_build(lw_expr_type_int, v);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
642 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
643
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
644 if (**p == '@' && (*((*p)+1) >= '0' && *((*p)+1) <= '7'))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
645 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
646 // octal constant
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
647 int v = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
648 (*p)++;
44
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
649 if (**p == '-')
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
650 {
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
651 (*p)++;
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
652 neg = -1;
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
653 }
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
654
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
655
354
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
656 if (!**p || !strchr("01234567", **p))
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
657 {
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
658 (*p)--;
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
659 if (neg < 0)
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
660 (*p)--;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
661 return NULL;
354
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
662 }
433851a26794 Make base prefix sigils error out if no number following
William Astle <lost@l-w.ca>
parents: 336
diff changeset
663
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
664 while (**p && strchr("01234567", **p))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
665 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
666 v = v * 8 + (**p - '0');
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
667 (*p)++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
668 }
44
1bff302e62a3 Accept negative sign after base prefix
lost@l-w.ca
parents: 38
diff changeset
669 return lw_expr_build(lw_expr_type_int, v * neg);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
670 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
671
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
672
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
673 // symbol or bare decimal or suffix constant here
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
674 do
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
675 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
676 int havedol = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
677 int l = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
678
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
679 while ((*p)[l] && strchr(SYMCHARS, (*p)[l]))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
680 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
681 if ((*p)[l] == '$')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
682 havedol = 1;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
683 l++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
684 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
685 if (l == 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
686 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
687
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
688 if ((*p)[l] == '{')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
689 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
690 while ((*p)[l] && (*p)[l] != '}')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
691 l++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
692 l++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
693 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
694
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
695 if (havedol || **p < '0' || **p > '9')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
696 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
697 // have a symbol here
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
698 char *sym;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
699 lw_expr_t term;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
700
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
701 sym = lw_strndup(*p, l);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
702 (*p) += l;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
703 term = lw_expr_build(lw_expr_type_var, sym);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
704 lw_free(sym);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
705 return term;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
706 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
707 } while (0);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
708
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
709 if (!**p)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
710 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
711
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
712 // we have a numeric constant here, either decimal or postfix base notation
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
713 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
714 int decval = 0, binval = 0, hexval = 0, octval = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
715 int valtype = 15; // 1 = bin, 2 = oct, 4 = dec, 8 = hex
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
716 int bindone = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
717 int val;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
718 int dval;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
719
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
720 while (1)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
721 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
722 if (!**p || !strchr("0123456789ABCDEFabcdefqhoQHO", **p))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
723 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
724 // we can legally be bin or decimal here
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
725 if (bindone)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
726 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
727 // just finished a binary value
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
728 val = binval;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
729 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
730 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
731 else if (valtype & 4)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
732 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
733 val = decval;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
734 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
735 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
736 else
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
737 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
738 // bad value
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
739 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
740 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
741 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
742
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
743 dval = toupper(**p);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
744 (*p)++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
745
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
746 if (bindone)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
747 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
748 // any characters past "B" means it is not binary
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
749 bindone = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
750 valtype &= 14;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
751 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
752
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
753 switch (dval)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
754 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
755 case 'Q':
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
756 case 'O':
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
757 if (valtype & 2)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
758 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
759 val = octval;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
760 valtype = -1;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
761 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
762 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
763 else
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
764 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
765 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
766 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
767 /* can't get here */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
768
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
769 case 'H':
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
770 if (valtype & 8)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
771 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
772 val = hexval;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
773 valtype = -1;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
774 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
775 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
776 else
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
777 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
778 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
779 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
780 /* can't get here */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
781
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
782 case 'B':
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
783 // this is a bit of a sticky one since B may be a
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
784 // hex number instead of the end of a binary number
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
785 // so it falls through to the digit case
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
786 if (valtype & 1)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
787 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
788 // could still be binary of hex
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
789 bindone = 1;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
790 valtype = 9;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
791 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
792 /* fall through intented */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
793
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
794 default:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
795 // digit
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
796 dval -= '0';
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
797 if (dval > 9)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
798 dval -= 7;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
799 if (valtype & 8)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
800 hexval = hexval * 16 + dval;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
801 if (valtype & 4)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
802 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
803 if (dval > 9)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
804 valtype &= 11;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
805 else
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
806 decval = decval * 10 + dval;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
807 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
808 if (valtype & 2)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
809 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
810 if (dval > 7)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
811 valtype &= 13;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
812 else
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
813 octval = octval * 8 + dval;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
814 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
815 if (valtype & 1)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
816 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
817 if (dval > 1)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
818 valtype &= 14;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
819 else
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
820 binval = binval * 2 + dval;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
821 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
822 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
823 if (valtype == -1)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
824 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
825
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
826 // return if no more valid types
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
827 if (valtype == 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
828 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
829
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
830 val = decval; // in case we fall through
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
831 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
832
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
833 // get here if we have a value
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
834 return lw_expr_build(lw_expr_type_int, val);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
835 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
836 // can't get here
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
837 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
838
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
839 lw_expr_t lwasm_parse_expr(asmstate_t *as, char **p)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
840 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
841 lw_expr_t e;
366
433dbc18fb41 Make byte overflow detection for 8 bit immediate not fail with COM operator
William Astle <lost@l-w.ca>
parents: 354
diff changeset
842
433dbc18fb41 Make byte overflow detection for 8 bit immediate not fail with COM operator
William Astle <lost@l-w.ca>
parents: 354
diff changeset
843 if (as->exprwidth != 16)
433dbc18fb41 Make byte overflow detection for 8 bit immediate not fail with COM operator
William Astle <lost@l-w.ca>
parents: 354
diff changeset
844 {
433dbc18fb41 Make byte overflow detection for 8 bit immediate not fail with COM operator
William Astle <lost@l-w.ca>
parents: 354
diff changeset
845 lw_expr_setwidth(as->exprwidth);
433dbc18fb41 Make byte overflow detection for 8 bit immediate not fail with COM operator
William Astle <lost@l-w.ca>
parents: 354
diff changeset
846 e = lw_expr_parse(p, as);
433dbc18fb41 Make byte overflow detection for 8 bit immediate not fail with COM operator
William Astle <lost@l-w.ca>
parents: 354
diff changeset
847 lw_expr_setwidth(0);
433dbc18fb41 Make byte overflow detection for 8 bit immediate not fail with COM operator
William Astle <lost@l-w.ca>
parents: 354
diff changeset
848 }
433dbc18fb41 Make byte overflow detection for 8 bit immediate not fail with COM operator
William Astle <lost@l-w.ca>
parents: 354
diff changeset
849 else
433dbc18fb41 Make byte overflow detection for 8 bit immediate not fail with COM operator
William Astle <lost@l-w.ca>
parents: 354
diff changeset
850 {
433dbc18fb41 Make byte overflow detection for 8 bit immediate not fail with COM operator
William Astle <lost@l-w.ca>
parents: 354
diff changeset
851 e = lw_expr_parse(p, as);
433dbc18fb41 Make byte overflow detection for 8 bit immediate not fail with COM operator
William Astle <lost@l-w.ca>
parents: 354
diff changeset
852 }
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
853 return e;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
854 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
855
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
856 int lwasm_reduce_expr(asmstate_t *as, lw_expr_t expr)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
857 {
216
398773d7e504 Fix crash bug on indexed expression handling
William Astle <lost@l-w.ca>
parents: 211
diff changeset
858 if (expr)
398773d7e504 Fix crash bug on indexed expression handling
William Astle <lost@l-w.ca>
parents: 211
diff changeset
859 lw_expr_simplify(expr, as);
2
7317fbe024af Clean up insane number of compiler warnings under -Wall
lost@l-w.ca
parents: 0
diff changeset
860 return 0;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
861 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
862
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
863 void lwasm_save_expr(line_t *cl, int id, lw_expr_t expr)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
864 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
865 struct line_expr_s *e;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
866
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
867 for (e = cl -> exprs; e; e = e -> next)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
868 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
869 if (e -> id == id)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
870 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
871 lw_expr_destroy(e -> expr);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
872 e -> expr = expr;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
873 return;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
874 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
875 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
876
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
877 e = lw_alloc(sizeof(struct line_expr_s));
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
878 e -> expr = expr;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
879 e -> id = id;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
880 e -> next = cl -> exprs;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
881 cl -> exprs = e;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
882 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
883
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
884 lw_expr_t lwasm_fetch_expr(line_t *cl, int id)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
885 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
886 struct line_expr_s *e;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
887
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
888 for (e = cl -> exprs; e; e = e -> next)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
889 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
890 if (e -> id == id)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
891 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
892 return e -> expr;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
893 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
894 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
895 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
896 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
897
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
898 void skip_operand(char **p)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
899 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
900 for (; **p && !isspace(**p); (*p)++)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
901 /* do nothing */ ;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
902 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
903
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
904 int lwasm_emitexpr(line_t *l, lw_expr_t expr, int size)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
905 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
906 int v = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
907 int ol;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
908
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
909 ol = l -> outputl;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
910 if (ol == -1)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
911 ol = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
912
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
913 if (lw_expr_istype(expr, lw_expr_type_int))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
914 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
915 v = lw_expr_intval(expr);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
916 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
917 // handle external/cross-section/incomplete references here
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
918 else
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
919 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
920 if (l -> as -> output_format == OUTPUT_OBJ)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
921 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
922 reloctab_t *re;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
923 lw_expr_t te;
13
c80e5a063967 Brought forward patch to fix segfault with output outside of a section
lost@l-w.ca
parents: 2
diff changeset
924
c80e5a063967 Brought forward patch to fix segfault with output outside of a section
lost@l-w.ca
parents: 2
diff changeset
925 if (l -> csect == NULL)
c80e5a063967 Brought forward patch to fix segfault with output outside of a section
lost@l-w.ca
parents: 2
diff changeset
926 {
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
927 lwasm_register_error(l -> as, l, E_INSTRUCTION_SECTION);
24
421d7ceb4d86 Fixed missing return value
lost@l-w.ca
parents: 13
diff changeset
928 return -1;
13
c80e5a063967 Brought forward patch to fix segfault with output outside of a section
lost@l-w.ca
parents: 2
diff changeset
929 }
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
930
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
931 if (size == 4)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
932 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
933 // create a two part reference because lwlink doesn't
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
934 // support 32 bit references
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
935 lw_expr_t te2;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
936 te = lw_expr_build(lw_expr_type_int, 0x10000);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
937 te2 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_divide, expr, te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
938 lw_expr_destroy(te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
939
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
940 re = lw_alloc(sizeof(reloctab_t));
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
941 re -> next = l -> csect -> reloctab;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
942 l -> csect -> reloctab = re;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
943 te = lw_expr_build(lw_expr_type_int, ol);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
944 re -> offset = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, l -> addr, te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
945 lw_expr_destroy(te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
946 lwasm_reduce_expr(l -> as, re -> offset);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
947 re -> expr = te2;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
948 re -> size = 2;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
949
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
950 te = lw_expr_build(lw_expr_type_int, 0xFFFF);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
951 te2 = lw_expr_build(lw_expr_type_oper, lw_expr_oper_bwand, expr, te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
952 lw_expr_destroy(te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
953
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
954 re = lw_alloc(sizeof(reloctab_t));
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
955 re -> next = l -> csect -> reloctab;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
956 l -> csect -> reloctab = re;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
957 te = lw_expr_build(lw_expr_type_int, ol + 2);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
958 re -> offset = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, l -> addr, te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
959 lw_expr_destroy(te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
960 lwasm_reduce_expr(l -> as, re -> offset);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
961 re -> expr = te2;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
962 re -> size = 2;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
963 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
964 else
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
965 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
966 // add "expression" record to section table
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
967 re = lw_alloc(sizeof(reloctab_t));
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
968 re -> next = l -> csect -> reloctab;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
969 l -> csect -> reloctab = re;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
970 te = lw_expr_build(lw_expr_type_int, ol);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
971 re -> offset = lw_expr_build(lw_expr_type_oper, lw_expr_oper_plus, l -> addr, te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
972 lw_expr_destroy(te);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
973 lwasm_reduce_expr(l -> as, re -> offset);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
974 re -> size = size;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
975 re -> expr = lw_expr_copy(expr);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
976 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
977 for (v = 0; v < size; v++)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
978 lwasm_emit(l, 0);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
979 return 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
980 }
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
981 lwasm_register_error(l->as, l, E_EXPRESSION_NOT_RESOLVED);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
982 return -1;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
983 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
984
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
985 switch (size)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
986 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
987 case 4:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
988 lwasm_emit(l, v >> 24);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
989 lwasm_emit(l, v >> 16);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
990 /* fallthrough intended */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
991
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
992 case 2:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
993 lwasm_emit(l, v >> 8);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
994 /* fallthrough intended */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
995
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
996 case 1:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
997 lwasm_emit(l, v);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
998 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
999
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1000 return 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1001 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1002
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1003 int lwasm_lookupreg2(const char *regs, char **p)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1004 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1005 int rval = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1006
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1007 while (*regs)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1008 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1009 if (toupper(**p) == *regs)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1010 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1011 if (regs[1] == ' ' && !isalpha(*(*p + 1)))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1012 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1013 if (toupper(*(*p + 1)) == regs[1])
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1014 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1015 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1016 regs += 2;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1017 rval++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1018 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1019 if (!*regs)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1020 return -1;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1021 if (regs[1] == ' ')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1022 (*p)++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1023 else
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1024 (*p) += 2;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1025 return rval;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1026 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1027
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1028 int lwasm_lookupreg3(const char *regs, char **p)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1029 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1030 int rval = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1031
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1032 while (*regs)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1033 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1034 if (toupper(**p) == *regs)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1035 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1036 if (regs[1] == ' ' && !isalpha(*(*p + 1)))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1037 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1038 if (toupper(*(*p + 1)) == regs[1])
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1039 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1040 if (regs[2] == ' ' && !isalpha(*(*p + 2)))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1041 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1042 if (toupper(*(*p + 2)) == regs[2])
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1043 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1044 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1045 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1046 regs += 3;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1047 rval++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1048 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1049 if (!*regs)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1050 return -1;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1051 if (regs[1] == ' ')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1052 (*p)++;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1053 else if (regs[2] == ' ')
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1054 (*p) += 2;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1055 else
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1056 (*p) += 3;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1057 return rval;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1058 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1059
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1060 void lwasm_show_errors(asmstate_t *as)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1061 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1062 line_t *cl;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1063 lwasm_error_t *e;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1064
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1065 for (cl = as -> line_head; cl; cl = cl -> next)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1066 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1067 if (!(cl -> err) && !(cl -> warn))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1068 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1069 for (e = cl -> err; e; e = e -> next)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1070 {
375
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
1071 fprintf(stderr, "ERROR: %s (%d)\n", e -> mess, e -> code);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1072 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1073 for (e = cl -> warn; e; e = e -> next)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1074 {
375
71f507f404f1 Add "testmode" pragma
William Astle <lost@l-w.ca>
parents: 374
diff changeset
1075 fprintf(stderr, "WARNING: %s (%d)\n", e -> mess, e -> code);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1076 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1077 fprintf(stderr, "%s:%05d %s\n\n", cl -> linespec, cl -> lineno, cl -> ltext);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1078 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1079 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1080
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1081 /*
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1082 this does any passes and other gymnastics that might be useful
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1083 to see if an expression reduces early
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1084 */
374
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 370
diff changeset
1085 void do_pass3(asmstate_t *as);
8e25147c2aa8 Clean up various "externs"
William Astle <lost@l-w.ca>
parents: 370
diff changeset
1086 void do_pass4_aux(asmstate_t *as, int force);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1087
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1088 void lwasm_interim_reduce(asmstate_t *as)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1089 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1090 do_pass3(as);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1091 // do_pass4_aux(as, 0);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1092 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1093
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1094 lw_expr_t lwasm_parse_cond(asmstate_t *as, char **p)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1095 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1096 lw_expr_t e;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1097
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1098 debug_message(as, 250, "Parsing condition");
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1099 e = lwasm_parse_expr(as, p);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1100 debug_message(as, 250, "COND EXPR: %s", lw_expr_print(e));
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1101
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1102 if (!e)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1103 {
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
1104 lwasm_register_error(as, as -> cl, E_EXPRESSION_BAD);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1105 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1106 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1107
210
5d969517db74 Added condundefzero pragma
William Astle <lost@l-w.ca>
parents: 209
diff changeset
1108 /* handle condundefzero */
5d969517db74 Added condundefzero pragma
William Astle <lost@l-w.ca>
parents: 209
diff changeset
1109 if (CURPRAGMA(as -> cl, PRAGMA_CONDUNDEFZERO))
5d969517db74 Added condundefzero pragma
William Astle <lost@l-w.ca>
parents: 209
diff changeset
1110 {
5d969517db74 Added condundefzero pragma
William Astle <lost@l-w.ca>
parents: 209
diff changeset
1111 as -> undefzero = 1;
5d969517db74 Added condundefzero pragma
William Astle <lost@l-w.ca>
parents: 209
diff changeset
1112 lwasm_reduce_expr(as, e);
5d969517db74 Added condundefzero pragma
William Astle <lost@l-w.ca>
parents: 209
diff changeset
1113 as -> undefzero = 0;
5d969517db74 Added condundefzero pragma
William Astle <lost@l-w.ca>
parents: 209
diff changeset
1114 }
5d969517db74 Added condundefzero pragma
William Astle <lost@l-w.ca>
parents: 209
diff changeset
1115
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1116 /* we need to simplify the expression here */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1117 debug_message(as, 250, "Doing interim reductions");
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1118 lwasm_interim_reduce(as);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1119 debug_message(as, 250, "COND EXPR: %s", lw_expr_print(e));
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1120 debug_message(as, 250, "Reducing expression");
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1121 lwasm_reduce_expr(as, e);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1122 debug_message(as, 250, "COND EXPR: %s", lw_expr_print(e));
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1123 /* lwasm_reduce_expr(as, e);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1124 debug_message(as, 250, "COND EXPR: %s", lw_expr_print(e));
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1125 lwasm_reduce_expr(as, e);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1126 debug_message(as, 250, "COND EXPR: %s", lw_expr_print(e));
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1127 lwasm_reduce_expr(as, e);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1128 debug_message(as, 250, "COND EXPR: %s", lw_expr_print(e));
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1129 */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1130
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1131 lwasm_save_expr(as -> cl, 4242, e);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1132
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1133 if (!lw_expr_istype(e, lw_expr_type_int))
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1134 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1135 debug_message(as, 250, "Non-constant expression");
370
8764142b3192 Convert internal error/warning handling framework to a new unified system
William Astle <lost@l-w.ca>
parents: 366
diff changeset
1136 lwasm_register_error(as, as -> cl, E_CONDITION_P1);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1137 return NULL;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1138 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1139 debug_message(as, 250, "Returning expression");
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1140 return e;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1141 }
241
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1142
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1143 struct range_data
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1144 {
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1145 int min;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1146 int max;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1147 asmstate_t *as;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1148 };
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1149 int lwasm_calculate_range(asmstate_t *as, lw_expr_t expr, int *min, int *max);
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1150 int lwasm_calculate_range_tf(lw_expr_t e, void *info)
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1151 {
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1152 struct range_data *rd = info;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1153 int i;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1154
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1155 if (lw_expr_istype(e, lw_expr_type_int))
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1156 {
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1157 i = lw_expr_intval(e);
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1158 rd -> min += i;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1159 rd -> max += i;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1160 return 0;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1161 }
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1162
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1163 if (lw_expr_istype(e, lw_expr_type_special))
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1164 {
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1165 line_t *l;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1166 if (lw_expr_specint(e) != lwasm_expr_linelen)
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1167 {
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1168 rd -> min = -1;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1169 return -1;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1170 }
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1171 l = (line_t *)lw_expr_specptr(e);
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1172 if (l -> len == -1)
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1173 {
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1174 rd -> min += l -> minlen;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1175 rd -> max += l -> maxlen;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1176 }
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1177 else
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1178 {
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1179 rd -> min += l -> len;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1180 }
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1181 return 0;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1182 }
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1183
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1184 if (lw_expr_istype(e, lw_expr_type_var))
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1185 {
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1186 lw_expr_t te;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1187 te = lw_expr_copy(e);
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1188 lwasm_reduce_expr(rd -> as, te);
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1189 if (lw_expr_istype(te, lw_expr_type_int))
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1190 {
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1191 i = lw_expr_intval(te);
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1192 rd -> min += i;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1193 rd -> max += i;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1194 }
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1195 else
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1196 {
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1197 rd -> min = -1;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1198 }
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1199 lw_expr_destroy(te);
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1200 if (rd -> min == -1)
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1201 return -1;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1202 return 0;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1203 }
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1204
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1205 if (lw_expr_istype(e, lw_expr_type_oper))
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1206 {
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1207 if (lw_expr_whichop(e) == lw_expr_oper_plus)
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1208 return 0;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1209 rd -> min = -1;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1210 return -1;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1211 }
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1212
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1213 rd -> min = -1;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1214 return -1;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1215 }
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1216
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1217 int lwasm_calculate_range(asmstate_t *as, lw_expr_t expr, int *min, int *max)
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1218 {
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1219 struct range_data rd;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1220
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1221 rd.min = 0;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1222 rd.max = 0;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1223 rd.as = as;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1224
256
bc25269d96bc Fix crash on expression range calculation
William Astle <lost@l-w.ca>
parents: 249
diff changeset
1225 if (!expr)
bc25269d96bc Fix crash on expression range calculation
William Astle <lost@l-w.ca>
parents: 249
diff changeset
1226 return -1;
bc25269d96bc Fix crash on expression range calculation
William Astle <lost@l-w.ca>
parents: 249
diff changeset
1227
241
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1228 lw_expr_testterms(expr, lwasm_calculate_range_tf, (void *)&rd);
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1229 *min = rd.min;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1230 *max = rd.max;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1231 if (rd.min == -1)
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1232 return -1;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1233 return 0;
d0e9dbe9afbe Add new heuristic for resolving instruction sizes.
William Astle <lost@l-w.ca>
parents: 240
diff changeset
1234 }
336
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1235
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1236 void lwasm_reduce_line_exprs(line_t *cl)
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1237 {
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1238 asmstate_t *as;
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1239 struct line_expr_s *le;
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1240 int i;
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1241
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1242 as = cl -> as;
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1243 as -> cl = cl;
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1244
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1245 // simplify address
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1246 lwasm_reduce_expr(as, cl -> addr);
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1247
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1248 // simplify data address
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1249 lwasm_reduce_expr(as, cl -> daddr);
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1250
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1251 // simplify each expression
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1252 for (i = 0, le = cl -> exprs; le; le = le -> next, i++)
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1253 {
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1254 lwasm_reduce_expr(as, le -> expr);
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1255 debug_message(as, 100, "Reduce expressions: exp[%d] = %s", i, lw_expr_print(le -> expr));
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1256 }
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1257
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1258 if (cl -> len == -1 || cl -> dlen == -1)
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1259 {
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1260 // try resolving the instruction length
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1261 // but don't force resolution
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1262 if (cl -> insn >= 0 && instab[cl -> insn].resolve)
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1263 {
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1264 (instab[cl -> insn].resolve)(as, cl, 0);
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1265 if ((cl -> inmod == 0) && cl -> len >= 0 && cl -> dlen >= 0)
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1266 {
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1267 if (cl -> len == 0)
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1268 cl -> len = cl -> dlen;
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1269 else
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1270 cl -> dlen = cl -> len;
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1271 }
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1272 }
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1273 }
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1274 debug_message(as, 100, "Reduce expressions: len = %d", cl -> len);
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1275 debug_message(as, 100, "Reduce expressions: dlen = %d", cl -> dlen);
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1276 debug_message(as, 100, "Reduce expressions: addr = %s", lw_expr_print(cl -> addr));
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1277 debug_message(as, 100, "Reduce expressions: daddr = %s", lw_expr_print(cl -> daddr));
30b2bad9b5eb Factor some code for simplifying lines so it can be reused
William Astle <lost@l-w.ca>
parents: 326
diff changeset
1278 }