Mercurial > projects > ldc
annotate dmd/mars.c @ 1013:8c73ff5f69e0
Use llvm::CallSite instead of custom CallOrInvoke class.
author | Frits van Bommel <fvbommel wxs.nl> |
---|---|
date | Sat, 28 Feb 2009 22:16:52 +0100 |
parents | 2667e3a145be |
children | f0b6549055ab |
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 | |
28 #include "mem.h" | |
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 | |
853
82ad6c0c601c
Add Elrood's patch to output LDC and LLVM source revs.
Christian Kamm <kamm incasoftware de>
parents:
849
diff
changeset
|
39 #include "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"; |
876
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
62 version = "v1.039"; |
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; |
82ad6c0c601c
Add Elrood's patch to output LDC and LLVM source revs.
Christian Kamm <kamm incasoftware de>
parents:
849
diff
changeset
|
64 llvm_version = LLVM_REV; |
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 | |
108 void verror(Loc loc, const char *format, va_list ap) | |
109 { | |
110 if (!global.gag) | |
111 { | |
112 char *p = loc.toChars(); | |
113 | |
114 if (*p) | |
115 fprintf(stdmsg, "%s: ", p); | |
116 mem.free(p); | |
117 | |
118 fprintf(stdmsg, "Error: "); | |
119 vfprintf(stdmsg, format, ap); | |
120 fprintf(stdmsg, "\n"); | |
121 fflush(stdmsg); | |
122 } | |
123 global.errors++; | |
124 } | |
125 | |
126 /*************************************** | |
127 * Call this after printing out fatal error messages to clean up and exit | |
128 * the compiler. | |
129 */ | |
130 | |
131 void fatal() | |
132 { | |
133 #if 0 | |
134 halt(); | |
135 #endif | |
136 exit(EXIT_FAILURE); | |
137 } | |
138 | |
139 /************************************** | |
140 * Try to stop forgetting to remove the breakpoints from | |
141 * release builds. | |
142 */ | |
143 void halt() | |
144 { | |
145 #ifdef DEBUG | |
146 *(char*)0=0; | |
147 #endif | |
148 } | |
149 | |
150 /*********************************** | |
151 * Parse and append contents of environment variable envvar | |
152 * to argc and argv[]. | |
153 * The string is separated into arguments, processing \ and ". | |
154 */ | |
155 | |
156 void getenv_setargv(const char *envvar, int *pargc, char** *pargv) | |
157 { | |
158 char *env; | |
159 char *p; | |
160 Array *argv; | |
161 int argc; | |
162 | |
163 int wildcard; // do wildcard expansion | |
164 int instring; | |
165 int slash; | |
166 char c; | |
167 int j; | |
168 | |
169 env = getenv(envvar); | |
170 if (!env) | |
171 return; | |
172 | |
173 env = mem.strdup(env); // create our own writable copy | |
174 | |
175 argc = *pargc; | |
176 argv = new Array(); | |
177 argv->setDim(argc); | |
178 | |
986
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
179 int argc_left = 0; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
180 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
|
181 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
|
182 // 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
|
183 global.params.run = true; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
184 // 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
|
185 argc_left = argc - i; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
186 argc = i; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
187 *pargv = &(*pargv)[i]; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
188 argv->setDim(i); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
189 break; |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
190 } else { |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
191 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
|
192 } |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
193 } |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
194 // 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
|
195 argv->push((char*)""); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
196 argc++; |
159 | 197 |
198 j = 1; // leave argv[0] alone | |
199 while (1) | |
200 { | |
201 wildcard = 1; | |
202 switch (*env) | |
203 { | |
204 case ' ': | |
205 case '\t': | |
206 env++; | |
207 break; | |
208 | |
209 case 0: | |
210 goto Ldone; | |
211 | |
212 case '"': | |
213 wildcard = 0; | |
214 default: | |
215 argv->push(env); // append | |
216 //argv->insert(j, env); // insert at position j | |
217 j++; | |
218 argc++; | |
219 p = env; | |
220 slash = 0; | |
221 instring = 0; | |
222 c = 0; | |
223 | |
224 while (1) | |
225 { | |
226 c = *env++; | |
227 switch (c) | |
228 { | |
229 case '"': | |
230 p -= (slash >> 1); | |
231 if (slash & 1) | |
232 { p--; | |
233 goto Laddc; | |
234 } | |
235 instring ^= 1; | |
236 slash = 0; | |
237 continue; | |
238 | |
239 case ' ': | |
240 case '\t': | |
241 if (instring) | |
242 goto Laddc; | |
243 *p = 0; | |
244 //if (wildcard) | |
245 //wildcardexpand(); // not implemented | |
246 break; | |
247 | |
248 case '\\': | |
249 slash++; | |
250 *p++ = c; | |
251 continue; | |
252 | |
253 case 0: | |
254 *p = 0; | |
255 //if (wildcard) | |
256 //wildcardexpand(); // not implemented | |
257 goto Ldone; | |
258 | |
259 default: | |
260 Laddc: | |
261 slash = 0; | |
262 *p++ = c; | |
263 continue; | |
264 } | |
265 break; | |
266 } | |
267 } | |
268 } | |
269 | |
270 Ldone: | |
986
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
271 assert(argc == argv->dim); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
272 argv->reserve(argc_left); |
a8cb25d478c4
Use LLVM-style command line (instead of DMD-style)
Frits van Bommel <fvbommel wxs.nl>
parents:
960
diff
changeset
|
273 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
|
274 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
|
275 |
159 | 276 *pargc = argc; |
277 *pargv = (char **)argv->data; | |
278 } |