annotate lwasm/pragma.c @ 352:f5b77989f675

Added PRAGMA and *PRAGMA
author lost@starbug
date Tue, 30 Mar 2010 20:56:54 -0600
parents 619fd6ad4ab9
children a9521955554f
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 },
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
42 { 0, 0 }
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
43 };
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
44
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
45 static const struct pragma_list reset_pragmas[] =
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
46 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
47 { "nodollarnotlocal", PRAGMA_DOLLARNOTLOCAL },
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
48 { "index0tonone", PRAGMA_NOINDEX0TONONE },
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
49 { "noundefextern", PRAGMA_UNDEFEXTERN },
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
50 { "nocescapes", PRAGMA_CESCAPES },
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
51 { "noimportundefexport", PRAGMA_IMPORTUNDEFEXPORT },
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
52 { 0, 0 }
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
53 };
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
54
352
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
55 int parse_pragma_string(asmstate_t *as, char *str, int ignoreerr)
325
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
56 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
57 char *p;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
58 int i;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
59 const char *np = str;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
60 int pragmas = as -> pragmas;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
61
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
62 while (np)
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
63 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
64 p = lw_token(np, ',', &np);
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
65 for (i = 0; set_pragmas[i].str; i++)
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
66 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
67 if (!strcasecmp(p, set_pragmas[i].str))
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
68 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
69 pragmas |= set_pragmas[i].flag;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
70 goto out;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
71 }
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
72 }
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
73 for (i = 0; reset_pragmas[i].str; i++)
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
74 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
75 if (!strcasecmp(p, reset_pragmas[i].str))
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
76 {
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
77 pragmas &= ~(reset_pragmas[i].flag);
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
78 goto out;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
79 }
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
80 }
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
81 /* unrecognized pragma here */
352
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
82 if (!ignoreerr)
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
83 {
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
84 lw_free(p);
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
85 return 0;
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
86 }
325
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
87 out:
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
88 lw_free(p);
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
89 }
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
90 as -> pragmas = pragmas;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
91 return 1;
619fd6ad4ab9 Completed command line parsing
lost
parents:
diff changeset
92 }
352
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
93
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
94 PARSEFUNC(pseudo_parse_pragma)
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
95 {
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
96 char *ps, *t;
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
97
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
98 for (t = *p; *t && !isspace(*t); t++)
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
99 /* do nothing */ ;
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
100
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
101 ps = lw_strndup(*p, t - *p);
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
102 *p = t;
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
103
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
104 if (parse_pragma_string(as, ps, 0) == 0)
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
105 {
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
106 lwasm_register_error(as, l, "Unrecognized pragma string");
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
107 }
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
108 lw_free(ps);
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
109 }
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
110
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
111 PARSEFUNC(pseudo_parse_starpragma)
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
112 {
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
113 char *ps, *t;
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
114
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
115 for (t = *p; *t && !isspace(*t); t++)
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
116 /* do nothing */ ;
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
117
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
118 ps = lw_strndup(*p, t - *p);
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
119 *p = t;
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
120
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
121 // *pragma must NEVER throw an error
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
122 parse_pragma_string(as, ps, 1);
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
123 lw_free(ps);
f5b77989f675 Added PRAGMA and *PRAGMA
lost@starbug
parents: 325
diff changeset
124 }