Mercurial > projects > ldc
annotate dmd/mars.c @ 1138:4c8bb03e4fbc
Update DtoConstFP() to be correct after LLVM r67562, which changed the way the
APFloat constructor expects its i80 APInts to be formatted. (They're now
actually consistent with the x87 format)
author | Frits van Bommel <fvbommel wxs.nl> |
---|---|
date | Tue, 24 Mar 2009 15:24:59 +0100 |
parents | eeb8b95ea92e |
children | e961851fb8be |
rev | line source |
---|---|
159 | 1 // Compiler implementation of the D programming language |
876
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
2 // Copyright (c) 1999-2009 by Digital Mars |
159 | 3 // All Rights Reserved |
4 // written by Walter Bright | |
5 // http://www.digitalmars.com | |
6 // License for redistribution is by either the Artistic License | |
7 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
8 // See the included readme.txt for details. | |
9 | |
10 #include <stdio.h> | |
11 #include <stdlib.h> | |
12 #include <ctype.h> | |
13 #include <assert.h> | |
14 #include <limits.h> | |
15 #include <string> | |
16 #include <cstdarg> | |
17 | |
18 #if __DMC__ | |
19 #include <dos.h> | |
20 #endif | |
21 | |
571
cbd6c8073a32
Changed all '#if linux || __APPLE__' to '#if POSIX' so we can support other platforms too, thanx for the suggestion anders.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
568
diff
changeset
|
22 #if POSIX |
159 | 23 #include <errno.h> |
432 | 24 #elif _WIN32 |
25 #include <windows.h> | |
159 | 26 #endif |
27 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
28 #include "rmem.h" |
159 | 29 #include "root.h" |
30 | |
31 #include "mars.h" | |
32 #include "module.h" | |
33 #include "mtype.h" | |
34 #include "id.h" | |
35 #include "cond.h" | |
36 #include "expression.h" | |
37 #include "lexer.h" | |
38 | |
1064
f0b6549055ab
Make LDC work with LLVM trunk (s/LinkOnceLinkage/LinkOnceOdrLinkage/)
Frits van Bommel <fvbommel wxs.nl>
parents:
988
diff
changeset
|
39 #include "gen/revisions.h" |
788
dce4b4ea2aee
Print llvm package string in version header. See #128.
Christian Kamm <kamm incasoftware de>
parents:
785
diff
changeset
|
40 |
159 | 41 Global global; |
42 | |
43 Global::Global() | |
44 { | |
45 mars_ext = "d"; | |
46 sym_ext = "d"; | |
47 hdr_ext = "di"; | |
48 doc_ext = "html"; | |
49 ddoc_ext = "ddoc"; | |
50 | |
663
6aaa3d3c1183
First part of rename to LDC.
Christian Kamm <kamm incasoftware de>
parents:
658
diff
changeset
|
51 // LDC |
159 | 52 ll_ext = "ll"; |
53 bc_ext = "bc"; | |
675
bfe5229f9d8e
Disable bc output by default. Remove -dis. Add -output-bc, -output-ll, -output-s.
Christian Kamm <kamm incasoftware de>
parents:
663
diff
changeset
|
54 s_ext = "s"; |
708
fd5665da3a27
Hopefully sensible command switch handling. Changed default ext to .o on Windows.
Christian Kamm <kamm incasoftware de>
parents:
700
diff
changeset
|
55 obj_ext = "o"; |
374
1f20b9f7de1b
Fix nativeobj extension for Windows.
Christian Kamm <kamm incasoftware de>
parents:
366
diff
changeset
|
56 #if _WIN32 |
708
fd5665da3a27
Hopefully sensible command switch handling. Changed default ext to .o on Windows.
Christian Kamm <kamm incasoftware de>
parents:
700
diff
changeset
|
57 obj_ext_alt = "obj"; |
159 | 58 #endif |
59 | |
876
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
60 copyright = "Copyright (c) 1999-2009 by Digital Mars and Tomas Lindquist Olsen"; |
159 | 61 written = "written by Walter Bright and Tomas Lindquist Olsen"; |
1133
eeb8b95ea92e
Cleanup DMD 1.041 merge.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1124
diff
changeset
|
62 version = "v1.041"; |
853
82ad6c0c601c
Add Elrood's patch to output LDC and LLVM source revs.
Christian Kamm <kamm incasoftware de>
parents:
849
diff
changeset
|
63 ldc_version = LDC_REV; |
1064
f0b6549055ab
Make LDC work with LLVM trunk (s/LinkOnceLinkage/LinkOnceOdrLinkage/)
Frits van Bommel <fvbommel wxs.nl>
parents:
988
diff
changeset
|
64 llvm_version = LLVM_REV_STR; |
159 | 65 global.structalign = 8; |
66 | |
986
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
67 // This should only be used as a global, so the other fields are |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
68 // automatically initialized to zero when the program is loaded. |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
69 // In particular, DO NOT zero-initialize .params here (like DMD |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
70 // does) because command-line options initialize some of those |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
71 // fields to non-zero defaults, and do so from constructors that |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
72 // may run before this one. |
159 | 73 } |
74 | |
75 char *Loc::toChars() const | |
76 { | |
77 OutBuffer buf; | |
78 | |
79 if (filename) | |
80 { | |
81 buf.printf("%s", filename); | |
82 } | |
83 | |
84 if (linnum) | |
85 buf.printf("(%d)", linnum); | |
86 buf.writeByte(0); | |
87 return (char *)buf.extractData(); | |
88 } | |
89 | |
90 Loc::Loc(Module *mod, unsigned linnum) | |
91 { | |
92 this->linnum = linnum; | |
93 this->filename = mod ? mod->srcfile->toChars() : NULL; | |
94 } | |
95 | |
96 /************************************** | |
97 * Print error message and exit. | |
98 */ | |
99 | |
100 void error(Loc loc, const char *format, ...) | |
101 { | |
102 va_list ap; | |
103 va_start(ap, format); | |
104 verror(loc, format, ap); | |
105 va_end( ap ); | |
106 } | |
107 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
108 void warning(Loc loc, const char *format, ...) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
109 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
110 if (global.params.warnings && !global.gag) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
111 { |
1124
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
112 va_list ap; |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
113 va_start(ap, format); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
114 vwarning(loc, format, ap); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
115 va_end( ap ); |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
116 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
117 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1064
diff
changeset
|
118 |
159 | 119 void verror(Loc loc, const char *format, va_list ap) |
120 { | |
121 if (!global.gag) | |
122 { | |
123 char *p = loc.toChars(); | |
124 | |
125 if (*p) | |
126 fprintf(stdmsg, "%s: ", p); | |
127 mem.free(p); | |
128 | |
129 fprintf(stdmsg, "Error: "); | |
130 vfprintf(stdmsg, format, ap); | |
131 fprintf(stdmsg, "\n"); | |
132 fflush(stdmsg); | |
133 } | |
134 global.errors++; | |
135 } | |
136 | |
1124
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
137 void vwarning(Loc loc, const char *format, va_list ap) |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
138 { |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
139 if (global.params.warnings && !global.gag) |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
140 { |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
141 char *p = loc.toChars(); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
142 |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
143 if (*p) |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
144 fprintf(stdmsg, "%s: ", p); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
145 mem.free(p); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
146 |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
147 fprintf(stdmsg, "Warning: "); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
148 vfprintf(stdmsg, format, ap); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
149 fprintf(stdmsg, "\n"); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
150 fflush(stdmsg); |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
151 } |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
152 } |
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
153 |
159 | 154 /*************************************** |
155 * Call this after printing out fatal error messages to clean up and exit | |
156 * the compiler. | |
157 */ | |
158 | |
159 void fatal() | |
160 { | |
161 #if 0 | |
162 halt(); | |
163 #endif | |
164 exit(EXIT_FAILURE); | |
165 } | |
166 | |
167 /************************************** | |
168 * Try to stop forgetting to remove the breakpoints from | |
169 * release builds. | |
170 */ | |
171 void halt() | |
172 { | |
173 #ifdef DEBUG | |
174 *(char*)0=0; | |
175 #endif | |
176 } | |
177 | |
178 /*********************************** | |
179 * Parse and append contents of environment variable envvar | |
180 * to argc and argv[]. | |
181 * The string is separated into arguments, processing \ and ". | |
182 */ | |
183 | |
184 void getenv_setargv(const char *envvar, int *pargc, char** *pargv) | |
185 { | |
186 char *env; | |
187 char *p; | |
188 Array *argv; | |
189 int argc; | |
190 | |
191 int wildcard; // do wildcard expansion | |
192 int instring; | |
193 int slash; | |
194 char c; | |
195 int j; | |
196 | |
197 env = getenv(envvar); | |
198 if (!env) | |
199 return; | |
200 | |
201 env = mem.strdup(env); // create our own writable copy | |
202 | |
203 argc = *pargc; | |
204 argv = new Array(); | |
205 argv->setDim(argc); | |
206 | |
986
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
207 int argc_left = 0; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
208 for (int i = 0; i < argc; i++) { |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
209 if (!strcmp((*pargv)[i], "-run") || !strcmp((*pargv)[i], "--run")) { |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
210 // HACK: set flag to indicate we saw '-run' here |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
211 global.params.run = true; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
212 // Don't eat -run yet so the program arguments don't get changed |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
213 argc_left = argc - i; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
214 argc = i; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
215 *pargv = &(*pargv)[i]; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
216 argv->setDim(i); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
217 break; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
218 } else { |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
219 argv->data[i] = (void *)(*pargv)[i]; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
220 } |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
221 } |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
222 // HACK to stop required values from command line being drawn from DFLAGS |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
223 argv->push((char*)""); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
224 argc++; |
159 | 225 |
226 j = 1; // leave argv[0] alone | |
227 while (1) | |
228 { | |
229 wildcard = 1; | |
230 switch (*env) | |
231 { | |
232 case ' ': | |
233 case '\t': | |
234 env++; | |
235 break; | |
236 | |
237 case 0: | |
238 goto Ldone; | |
239 | |
240 case '"': | |
241 wildcard = 0; | |
242 default: | |
243 argv->push(env); // append | |
244 //argv->insert(j, env); // insert at position j | |
245 j++; | |
246 argc++; | |
247 p = env; | |
248 slash = 0; | |
249 instring = 0; | |
250 c = 0; | |
251 | |
252 while (1) | |
253 { | |
254 c = *env++; | |
255 switch (c) | |
256 { | |
257 case '"': | |
258 p -= (slash >> 1); | |
259 if (slash & 1) | |
260 { p--; | |
261 goto Laddc; | |
262 } | |
263 instring ^= 1; | |
264 slash = 0; | |
265 continue; | |
266 | |
267 case ' ': | |
268 case '\t': | |
269 if (instring) | |
270 goto Laddc; | |
271 *p = 0; | |
272 //if (wildcard) | |
273 //wildcardexpand(); // not implemented | |
274 break; | |
275 | |
276 case '\\': | |
277 slash++; | |
278 *p++ = c; | |
279 continue; | |
280 | |
281 case 0: | |
282 *p = 0; | |
283 //if (wildcard) | |
284 //wildcardexpand(); // not implemented | |
285 goto Ldone; | |
286 | |
287 default: | |
288 Laddc: | |
289 slash = 0; | |
290 *p++ = c; | |
291 continue; | |
292 } | |
293 break; | |
294 } | |
295 } | |
296 } | |
297 | |
298 Ldone: | |
986
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
299 assert(argc == argv->dim); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
300 argv->reserve(argc_left); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
301 for (int i = 0; i < argc_left; i++) |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
302 argv->data[argc++] = (void *)(*pargv)[i]; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
303 |
159 | 304 *pargc = argc; |
305 *pargv = (char **)argv->data; | |
306 } |