annotate lwasm/pragma.c @ 448:5cccf90bf838 3.0 tip

Fixed bug with complex external references generating invalid relocations in the object file
author lost@l-w.ca
date Fri, 05 Nov 2010 22:27:00 -0600
parents a9521955554f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
325
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
1 /*
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
2 pragma.c
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
3
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
4 Copyright © 2010 William Astle
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
5
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
6 This file is part of LWTOOLS.
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
7
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
11 version.
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
12
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
16 more details.
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
17
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
20 */
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
21
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
22 #include <config.h>
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
23
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
24 #include <lw_string.h>
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
25
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
26 #include "lwasm.h"
352
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
27 #include "instab.h"
325
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
28
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
29 struct pragma_list
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
30 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
31 const char *str;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
32 int flag;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
33 };
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
34
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
35 static const struct pragma_list set_pragmas[] =
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
36 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
37 { "dollarnotlocal", PRAGMA_DOLLARNOTLOCAL },
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
38 { "noindex0tonone", PRAGMA_NOINDEX0TONONE },
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
39 { "undefextern", PRAGMA_UNDEFEXTERN },
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
40 { "cescapes", PRAGMA_CESCAPES },
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
41 { "importundefexport", PRAGMA_IMPORTUNDEFEXPORT },
442
a9521955554f Added pragma pcaspcr to treat PC as PCR; additional fixes for PCR addressing modes
lost@l-w.ca
parents: 352
diff changeset
42 { "pcaspcr", PRAGMA_PCASPCR },
325
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
43 { 0, 0 }
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
44 };
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
45
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
46 static const struct pragma_list reset_pragmas[] =
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
47 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
48 { "nodollarnotlocal", PRAGMA_DOLLARNOTLOCAL },
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
49 { "index0tonone", PRAGMA_NOINDEX0TONONE },
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
50 { "noundefextern", PRAGMA_UNDEFEXTERN },
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
51 { "nocescapes", PRAGMA_CESCAPES },
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
52 { "noimportundefexport", PRAGMA_IMPORTUNDEFEXPORT },
442
a9521955554f Added pragma pcaspcr to treat PC as PCR; additional fixes for PCR addressing modes
lost@l-w.ca
parents: 352
diff changeset
53 { "nopcaspcr", PRAGMA_PCASPCR },
325
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
54 { 0, 0 }
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
55 };
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
56
352
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
57 int parse_pragma_string(asmstate_t *as, char *str, int ignoreerr)
325
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
58 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
59 char *p;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
60 int i;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
61 const char *np = str;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
62 int pragmas = as -> pragmas;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
63
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
64 while (np)
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
65 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
66 p = lw_token(np, ',', &np);
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
67 for (i = 0; set_pragmas[i].str; i++)
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
68 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
69 if (!strcasecmp(p, set_pragmas[i].str))
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
70 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
71 pragmas |= set_pragmas[i].flag;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
72 goto out;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
73 }
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
74 }
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
75 for (i = 0; reset_pragmas[i].str; i++)
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
76 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
77 if (!strcasecmp(p, reset_pragmas[i].str))
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
78 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
79 pragmas &= ~(reset_pragmas[i].flag);
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
80 goto out;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
81 }
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
82 }
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
83 /* unrecognized pragma here */
352
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
84 if (!ignoreerr)
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
85 {
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
86 lw_free(p);
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
87 return 0;
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
88 }
325
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
89 out:
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
90 lw_free(p);
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
91 }
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
92 as -> pragmas = pragmas;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
93 return 1;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
94 }
352
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
95
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
96 PARSEFUNC(pseudo_parse_pragma)
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
97 {
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
98 char *ps, *t;
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
99
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
100 for (t = *p; *t && !isspace(*t); t++)
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
101 /* do nothing */ ;
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
102
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
103 ps = lw_strndup(*p, t - *p);
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
104 *p = t;
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
105
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
106 if (parse_pragma_string(as, ps, 0) == 0)
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
107 {
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
108 lwasm_register_error(as, l, "Unrecognized pragma string");
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
109 }
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
110 lw_free(ps);
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
111 }
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
112
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
113 PARSEFUNC(pseudo_parse_starpragma)
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
114 {
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
115 char *ps, *t;
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
116
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
117 for (t = *p; *t && !isspace(*t); t++)
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
118 /* do nothing */ ;
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
119
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
120 ps = lw_strndup(*p, t - *p);
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
121 *p = t;
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
122
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
123 // *pragma must NEVER throw an error
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
124 parse_pragma_string(as, ps, 1);
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
125 lw_free(ps);
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
126 }