Mercurial > projects > ddmd
annotate dmd/Module.d @ 73:ef02e2e203c2
Updating to dmd2.033
author | korDen |
---|---|
date | Sat, 28 Aug 2010 19:42:41 +0400 |
parents | 2e2a5c3f943a |
children | 7e0d548de9e6 |
rev | line source |
---|---|
0 | 1 module dmd.Module; |
2 | |
3 import dmd.Package; | |
4 import dmd.DsymbolTable; | |
5 import dmd.backend.TYM; | |
6 import dmd.Array; | |
7 import dmd.StaticDtorDeclaration; | |
8 import dmd.Scope; | |
9 import dmd.Id; | |
10 import dmd.Import; | |
11 import dmd.ClassDeclaration; | |
12 import dmd.ModuleDeclaration; | |
13 import dmd.File; | |
14 import dmd.Identifier; | |
15 import dmd.Dsymbol; | |
16 import dmd.ModuleInfoDeclaration; | |
17 import dmd.FuncDeclaration; | |
18 import dmd.Loc; | |
19 import dmd.Macro; | |
20 import dmd.Escape; | |
21 import dmd.OutBuffer; | |
22 import dmd.HdrGenState; | |
23 import dmd.ArrayTypes; | |
24 import dmd.FileName; | |
25 import dmd.Global; | |
26 import dmd.Parser; | |
27 import dmd.Lexer; | |
28 import dmd.Util; | |
29 import dmd.String; | |
30 import dmd.ScopeDsymbol; | |
31 import dmd.Type; | |
32 import dmd.backend.TYPE; | |
33 import dmd.backend.Cstate; | |
34 import dmd.backend.OPER; | |
35 import dmd.backend.REG; | |
36 import dmd.backend.Symbol; | |
37 import dmd.backend.elem; | |
38 import dmd.backend.mTYman; | |
39 import dmd.backend.Util; | |
40 import dmd.backend.SC; | |
41 import dmd.backend.FL; | |
42 import dmd.backend.SFL; | |
43 import dmd.backend.TF; | |
44 import dmd.backend.RTLSYM; | |
45 import dmd.backend.BC; | |
46 import dmd.backend.block; | |
47 import dmd.backend.targ_types; | |
48 import dmd.backend.dt_t; | |
49 import dmd.backend.TYM; | |
50 import dmd.backend.Util; | |
51 import dmd.backend.Classsym; | |
52 import dmd.backend.glue; | |
53 import dmd.backend.LIST; | |
54 import dmd.codegen.Util; | |
55 | |
56 import core.stdc.string; | |
57 import core.stdc.stdlib; | |
58 | |
4 | 59 import core.memory; |
2 | 60 |
0 | 61 uint readwordLE(ushort* p) |
62 { | |
63 version (__I86__) { | |
64 return *p; | |
65 } else { | |
66 return ((cast(ubyte*)p)[1] << 8) | (cast(ubyte*)p)[0]; | |
67 } | |
68 } | |
69 | |
70 uint readwordBE(ushort* p) | |
71 { | |
72 return ((cast(ubyte*)p)[0] << 8) | (cast(ubyte*)p)[1]; | |
73 } | |
74 | |
75 uint readlongLE(uint* p) | |
76 { | |
77 version (__I86__) { | |
78 return *p; | |
79 } else { | |
80 return (cast(ubyte*)p)[0] | | |
81 ((cast(ubyte*)p)[1] << 8) | | |
82 ((cast(ubyte*)p)[2] << 16) | | |
83 ((cast(ubyte*)p)[3] << 24); | |
84 } | |
85 } | |
86 | |
87 uint readlongBE(uint* p) | |
88 { | |
89 return (cast(ubyte*)p)[3] | | |
90 ((cast(ubyte*)p)[2] << 8) | | |
91 ((cast(ubyte*)p)[1] << 16) | | |
92 ((cast(ubyte*)p)[0] << 24); | |
93 } | |
94 | |
95 /* Segments */ | |
96 enum Segment { | |
97 CODE = 1, /* code segment */ | |
98 DATA = 2, /* initialized data */ | |
99 CDATA = 3, /* constant data */ | |
100 UDATA = 4, /* uninitialized data */ | |
101 UNKNOWN = -1, /* unknown segment */ | |
102 } | |
103 | |
104 struct seg_data | |
105 { | |
106 int SDseg; // omf file segment index | |
107 targ_size_t SDoffset; // starting offset for data | |
108 | |
109 bool isfarseg; | |
110 int seg; // segment number | |
111 int lnameidx; // lname idx of segment name | |
112 int classidx; // lname idx of class name | |
113 uint attr; // segment attribute | |
114 targ_size_t origsize; // original size | |
115 int seek; // seek position in output file | |
116 } | |
117 | |
118 extern (C) extern __gshared seg_data** SegData; | |
119 | |
120 ref targ_size_t Offset(Segment seg) { | |
121 return SegData[seg].SDoffset; | |
122 } | |
123 | |
124 ref targ_size_t Doffset() { | |
125 return SegData[Segment.DATA].SDoffset; | |
126 } | |
127 | |
128 ref targ_size_t CDoffset() { | |
129 return SegData[Segment.CDATA].SDoffset; | |
130 } | |
131 | |
132 ref targ_size_t UDoffset() { | |
133 return SegData[Segment.UDATA].SDoffset; | |
134 } | |
135 | |
136 enum CF { | |
137 CFes = 1, // generate an ES: segment override for this instr | |
138 CFjmp16 = 2, // need 16 bit jump offset (long branch) | |
139 CFtarg = 4, // this code is the target of a jump | |
140 CFseg = 8, // get segment of immediate value | |
141 CFoff = 0x10, // get offset of immediate value | |
142 CFss = 0x20, // generate an SS: segment override (not with | |
143 // CFes at the same time, though!) | |
144 CFpsw = 0x40, // we need the flags result after this instruction | |
145 CFopsize = 0x80, // prefix with operand size | |
146 CFaddrsize = 0x100, // prefix with address size | |
147 CFds = 0x200, // need DS override (not with es, ss, or cs ) | |
148 CFcs = 0x400, // need CS override | |
149 CFfs = 0x800, // need FS override | |
150 CFgs = (CFcs | CFfs), // need GS override | |
151 CFwait = 0x1000, // If I32 it indicates when to output a WAIT | |
152 CFselfrel = 0x2000, // if self-relative | |
153 CFunambig = 0x4000, // indicates cannot be accessed by other addressing | |
154 // modes | |
155 CFtarg2 = 0x8000, // like CFtarg, but we can't optimize this away | |
156 CFvolatile = 0x10000, // volatile reference, do not schedule | |
157 CFclassinit = 0x20000, // class init code | |
158 | |
159 CFSEG = (CFes | CFss | CFds | CFcs | CFfs | CFgs), | |
160 CFPREFIX = (CFSEG | CFopsize | CFaddrsize), | |
161 } | |
162 | |
163 class Module : Package | |
164 { | |
165 static Module rootModule; | |
166 static DsymbolTable modules; // symbol table of all modules | |
167 static Array amodules; // array of all modules | |
168 static Array deferred; // deferred Dsymbol's needing semantic() run on them | |
169 static uint dprogress; // progress resolving the deferred list | |
170 | |
171 static void init() | |
172 { | |
173 modules = new DsymbolTable(); | |
174 amodules = new Array(); | |
175 deferred = new Array(); | |
176 } | |
177 | |
178 static ClassDeclaration moduleinfo; | |
179 | |
180 | |
181 string arg; // original argument name | |
182 ModuleDeclaration md; // if !null, the contents of the ModuleDeclaration declaration | |
183 File srcfile; // input source file | |
184 File objfile; // output .obj file | |
185 File hdrfile; // 'header' file | |
186 File symfile; // output symbol file | |
187 File docfile; // output documentation file | |
188 uint errors; // if any errors in file | |
189 uint numlines; // number of lines in source file | |
190 int isHtml; // if it is an HTML file | |
191 int isDocFile; // if it is a documentation input file, not D source | |
192 int needmoduleinfo; /// TODO: change to bool | |
193 version (IN_GCC) { | |
194 int strictlyneedmoduleinfo; | |
195 } | |
196 | |
197 int selfimports; // 0: don't know, 1: does not, 2: does | |
198 int selfImports() // returns !=0 if module imports itself | |
199 { | |
200 assert(false); | |
201 } | |
202 | |
203 int insearch; | |
204 Identifier searchCacheIdent; | |
205 Dsymbol searchCacheSymbol; // cached value of search | |
206 int searchCacheFlags; // cached flags | |
207 | |
208 int semanticstarted; // has semantic() been started? | |
209 int semanticRun; // has semantic() been done? | |
210 int root; // != 0 if this is a 'root' module, | |
211 // i.e. a module that will be taken all the | |
212 // way to an object file | |
213 Module importedFrom; // module from command line we're imported from, | |
214 // i.e. a module that will be taken all the | |
215 // way to an object file | |
216 | |
217 Array decldefs; // top level declarations for this Module | |
218 | |
219 Array aimports; // all imported modules | |
220 | |
221 ModuleInfoDeclaration vmoduleinfo; | |
222 | |
223 uint debuglevel; // debug level | |
224 Array debugids; // debug identifiers | |
225 Array debugidsNot; // forward referenced debug identifiers | |
226 | |
227 uint versionlevel; // version level | |
228 Array versionids; // version identifiers | |
229 Array versionidsNot; // forward referenced version identifiers | |
230 | |
231 Macro macrotable; // document comment macros | |
232 Escape escapetable; // document comment escapes | |
233 bool safe; // TRUE if module is marked as 'safe' | |
234 | |
235 this(string filename, Identifier ident, int doDocComment, int doHdrGen) | |
236 { | |
237 super(ident); | |
238 FileName objfilename; | |
239 | |
240 aimports = new Array(); | |
241 | |
242 //writef("Module.Module(filename = '%s', ident = '%s')\n", filename, ident.toChars()); | |
243 this.arg = filename; | |
244 | |
245 FileName srcfilename = FileName.defaultExt(filename, global.mars_ext); | |
246 if (!srcfilename.equalsExt(global.mars_ext) && | |
247 !srcfilename.equalsExt(global.hdr_ext) && | |
248 !srcfilename.equalsExt("dd")) | |
249 { | |
250 if (srcfilename.equalsExt("html") || | |
251 srcfilename.equalsExt("htm") || | |
252 srcfilename.equalsExt("xhtml")) | |
253 { | |
254 if (!global.params.useDeprecated) | |
255 error("html source files is deprecated %s", srcfilename.toChars()); | |
256 isHtml = 1; | |
257 } | |
258 else | |
259 { | |
260 error("source file name '%s' must have .%s extension", srcfilename.toChars(), global.mars_ext); | |
261 fatal(); | |
262 } | |
263 } | |
264 | |
265 string argobj; | |
266 if (global.params.objname) | |
267 argobj = global.params.objname; | |
268 else if (global.params.preservePaths) | |
269 argobj = filename; | |
270 else | |
271 argobj = FileName.name(filename); | |
272 if (!FileName.absolute(argobj)) | |
273 { | |
274 argobj = FileName.combine(global.params.objdir, argobj); | |
275 } | |
276 | |
277 if (global.params.objname) | |
278 objfilename = new FileName(argobj); | |
279 else | |
280 objfilename = FileName.forceExt(argobj, global.obj_ext); | |
281 | |
282 FileName symfilename = FileName.forceExt(filename, global.sym_ext); | |
283 | |
284 srcfile = new File(srcfilename); | |
285 | |
286 if (doDocComment) { | |
287 setDocfile(); | |
288 } | |
289 | |
290 if (doHdrGen) { | |
291 setHdrfile(); | |
292 } | |
293 | |
294 objfile = new File(objfilename); | |
295 symfile = new File(symfilename); | |
296 } | |
297 | |
298 static Module load(Loc loc, Array packages, Identifier ident) | |
299 { | |
300 Module m; | |
301 string filename; | |
302 | |
303 //writef("Module.load(ident = '%s')\n", ident.toChars()); | |
304 | |
305 // Build module filename by turning: | |
306 // foo.bar.baz | |
307 // into: | |
308 // foo\bar\baz | |
309 filename = ident.toChars(); | |
310 if (packages && packages.dim) | |
311 { | |
312 scope OutBuffer buf = new OutBuffer(); | |
313 int i; | |
314 | |
315 for (i = 0; i < packages.dim; i++) | |
316 { | |
317 Identifier pid = cast(Identifier)packages.data[i]; | |
318 | |
319 buf.writestring(pid.toChars()); | |
320 version (_WIN32) { | |
321 buf.writeByte('\\'); | |
322 } else { | |
323 buf.writeByte('/'); | |
324 } | |
325 } | |
326 buf.writestring(filename); | |
327 filename = buf.extractString(); | |
328 } | |
329 | |
330 m = new Module(filename, ident, 0, 0); | |
331 m.loc = loc; | |
332 | |
333 /* Search along global.path for .di file, then .d file. | |
334 */ | |
335 string result = null; | |
336 FileName fdi = FileName.forceExt(filename, global.hdr_ext); | |
337 FileName fd = FileName.forceExt(filename, global.mars_ext); | |
338 string sdi = fdi.toChars(); | |
339 string sd = fd.toChars(); | |
340 | |
341 if (FileName.exists(sdi)) { | |
342 result = sdi; | |
343 } else if (FileName.exists(sd)) { | |
344 result = sd; | |
345 } else if (FileName.absolute(filename)) { | |
346 ; | |
347 } | |
348 else | |
349 { | |
350 foreach (p; global.path) | |
351 { | |
352 string n = FileName.combine(p, sdi); | |
353 | |
354 if (FileName.exists(n)) | |
355 { | |
356 result = n; | |
357 break; | |
358 } | |
359 | |
360 n = FileName.combine(p, sd); | |
361 if (FileName.exists(n)) | |
362 { | |
363 result = n; | |
364 break; | |
365 } | |
366 } | |
367 } | |
368 | |
369 if (result) | |
370 m.srcfile = new File(result); | |
371 | |
372 if (global.params.verbose) | |
373 { | |
374 writef("import "); | |
375 if (packages) | |
376 { | |
377 for (size_t i = 0; i < packages.dim; i++) | |
378 { | |
379 Identifier pid = cast(Identifier)packages.data[i]; | |
380 writef("%s.", pid.toChars()); | |
381 } | |
382 } | |
383 writef("%s\t(%s)\n", ident.toChars(), m.srcfile.toChars()); | |
384 } | |
385 | |
386 m.read(loc); | |
387 m.parse(); | |
388 | |
389 version (IN_GCC) { | |
390 d_gcc_magic_module(m); | |
391 } | |
392 | |
393 return m; | |
394 } | |
395 | |
72 | 396 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 397 { |
398 assert(false); | |
399 } | |
400 | |
72 | 401 override string kind() |
0 | 402 { |
403 return "module"; | |
404 } | |
405 | |
406 void setDocfile() // set docfile member | |
407 { | |
408 assert(false); | |
409 } | |
410 | |
411 void read(Loc loc) // read file | |
412 { | |
413 //writef("Module.read('%s') file '%s'\n", toChars(), srcfile.toChars()); | |
414 if (srcfile.read()) | |
415 { | |
416 error(loc, "cannot read file '%s'", srcfile.toChars()); | |
417 fatal(); | |
418 } | |
419 } | |
420 | |
421 version (IN_GCC) { | |
422 void parse(bool dump_source = false) // syntactic parse | |
423 { | |
424 assert(false); | |
425 } | |
426 } else { | |
427 void parse() // syntactic parse | |
428 { | |
429 uint le; | |
430 uint bom; | |
431 | |
432 //printf("Module.parse()\n"); | |
433 | |
434 string srcname = srcfile.name.toChars(); | |
435 //printf("Module.parse(srcname = '%s')\n", srcname); | |
436 | |
437 ubyte* buf = srcfile.buffer; | |
438 uint buflen = srcfile.len; | |
439 | |
440 if (buflen >= 2) | |
441 { | |
442 /* Convert all non-UTF-8 formats to UTF-8. | |
443 * BOM : http://www.unicode.org/faq/utf_bom.html | |
444 * 00 00 FE FF UTF-32BE, big-endian | |
445 * FF FE 00 00 UTF-32LE, little-endian | |
446 * FE FF UTF-16BE, big-endian | |
447 * FF FE UTF-16LE, little-endian | |
448 * EF BB BF UTF-8 | |
449 */ | |
450 | |
451 bom = 1; // assume there's a BOM | |
452 if (buf[0] == 0xFF && buf[1] == 0xFE) | |
453 { | |
454 if (buflen >= 4 && buf[2] == 0 && buf[3] == 0) | |
455 { // UTF-32LE | |
456 le = 1; | |
457 | |
458 Lutf32: | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
459 OutBuffer dbuf = new OutBuffer(); |
0 | 460 uint* pu = cast(uint*)buf; |
461 uint* pumax = &pu[buflen / 4]; | |
462 | |
463 if (buflen & 3) | |
464 { error("odd length of UTF-32 char source %u", buflen); | |
465 fatal(); | |
466 } | |
467 | |
468 dbuf.reserve(buflen / 4); | |
469 for (pu += bom; pu < pumax; pu++) | |
470 { | |
471 uint u = le ? readlongLE(pu) : readlongBE(pu); | |
472 if (u & ~0x7F) | |
473 { | |
474 if (u > 0x10FFFF) | |
475 { error("UTF-32 value %08x greater than 0x10FFFF", u); | |
476 fatal(); | |
477 } | |
478 dbuf.writeUTF8(u); | |
479 } | |
480 else | |
481 dbuf.writeByte(u); | |
482 } | |
483 dbuf.writeByte(0); // add 0 as sentinel for scanner | |
484 buflen = dbuf.offset - 1; // don't include sentinel in count | |
485 buf = cast(ubyte*) dbuf.extractData(); | |
486 } | |
487 else | |
488 { | |
489 // UTF-16LE (X86) | |
490 // Convert it to UTF-8 | |
491 le = 1; | |
492 | |
493 Lutf16: | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
14
diff
changeset
|
494 OutBuffer dbuf = new OutBuffer(); |
0 | 495 ushort* pu = cast(ushort*)(buf); |
496 ushort *pumax = &pu[buflen / 2]; | |
497 | |
498 if (buflen & 1) | |
499 { error("odd length of UTF-16 char source %u", buflen); | |
500 fatal(); | |
501 } | |
502 | |
503 dbuf.reserve(buflen / 2); | |
504 for (pu += bom; pu < pumax; pu++) | |
505 { | |
506 uint u = le ? readwordLE(pu) : readwordBE(pu); | |
507 if (u & ~0x7F) | |
508 { | |
509 if (u >= 0xD800 && u <= 0xDBFF) | |
510 { uint u2; | |
511 | |
512 if (++pu > pumax) | |
513 { error("surrogate UTF-16 high value %04x at EOF", u); | |
514 fatal(); | |
515 } | |
516 u2 = le ? readwordLE(pu) : readwordBE(pu); | |
517 if (u2 < 0xDC00 || u2 > 0xDFFF) | |
518 { error("surrogate UTF-16 low value %04x out of range", u2); | |
519 fatal(); | |
520 } | |
521 u = (u - 0xD7C0) << 10; | |
522 u |= (u2 - 0xDC00); | |
523 } | |
524 else if (u >= 0xDC00 && u <= 0xDFFF) | |
525 { | |
526 error("unpaired surrogate UTF-16 value %04x", u); | |
527 fatal(); | |
528 } | |
529 else if (u == 0xFFFE || u == 0xFFFF) | |
530 { | |
531 error("illegal UTF-16 value %04x", u); | |
532 fatal(); | |
533 } | |
534 dbuf.writeUTF8(u); | |
535 } | |
536 else | |
537 dbuf.writeByte(u); | |
538 } | |
539 dbuf.writeByte(0); // add 0 as sentinel for scanner | |
540 buflen = dbuf.offset - 1; // don't include sentinel in count | |
541 buf = cast(ubyte*) dbuf.extractData(); | |
542 } | |
543 } | |
544 else if (buf[0] == 0xFE && buf[1] == 0xFF) | |
545 { // UTF-16BE | |
546 le = 0; | |
547 goto Lutf16; | |
548 } | |
549 else if (buflen >= 4 && buf[0] == 0 && buf[1] == 0 && buf[2] == 0xFE && buf[3] == 0xFF) | |
550 { // UTF-32BE | |
551 le = 0; | |
552 goto Lutf32; | |
553 } | |
554 else if (buflen >= 3 && buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF) | |
555 { // UTF-8 | |
556 | |
557 buf += 3; | |
558 buflen -= 3; | |
559 } | |
560 else | |
561 { | |
562 /* There is no BOM. Make use of Arcane Jill's insight that | |
563 * the first char of D source must be ASCII to | |
564 * figure out the encoding. | |
565 */ | |
566 | |
567 bom = 0; | |
568 if (buflen >= 4) | |
569 { if (buf[1] == 0 && buf[2] == 0 && buf[3] == 0) | |
570 { // UTF-32LE | |
571 le = 1; | |
572 goto Lutf32; | |
573 } | |
574 else if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0) | |
575 { // UTF-32BE | |
576 le = 0; | |
577 goto Lutf32; | |
578 } | |
579 } | |
580 if (buflen >= 2) | |
581 { | |
582 if (buf[1] == 0) | |
583 { // UTF-16LE | |
584 le = 1; | |
585 goto Lutf16; | |
586 } | |
587 else if (buf[0] == 0) | |
588 { // UTF-16BE | |
589 le = 0; | |
590 goto Lutf16; | |
591 } | |
592 } | |
593 | |
594 // It's UTF-8 | |
595 if (buf[0] >= 0x80) | |
596 { error("source file must start with BOM or ASCII character, not \\x%02X", buf[0]); | |
597 fatal(); | |
598 } | |
599 } | |
600 } | |
601 | |
602 version (IN_GCC) { | |
603 // dump utf-8 encoded source | |
604 if (dump_source) | |
605 { // %% srcname could contain a path ... | |
606 d_gcc_dump_source(srcname, "utf-8", buf, buflen); | |
607 } | |
608 } | |
609 | |
610 /* If it starts with the string "Ddoc", then it's a documentation | |
611 * source file. | |
612 */ | |
613 if (buflen >= 4 && memcmp(buf, "Ddoc".ptr, 4) == 0) | |
614 { | |
615 comment = buf + 4; | |
616 isDocFile = 1; | |
617 if (!docfile) | |
618 setDocfile(); | |
619 return; | |
620 } | |
621 if (isHtml) | |
622 { | |
623 assert(false); | |
624 ///OutBuffer dbuf = new OutBuffer(); | |
625 ///Html h = new Html(srcname, buf, buflen); | |
626 ///h.extractCode(dbuf); | |
627 ///buf = dbuf.data; | |
628 ///buflen = dbuf.offset; | |
629 | |
630 version (IN_GCC) { | |
631 // dump extracted source | |
632 ///if (dump_source) | |
633 /// d_gcc_dump_source(srcname, "d.utf-8", buf, buflen); | |
634 } | |
635 } | |
636 | |
637 Parser p = new Parser(this, buf, buflen, docfile !is null); | |
638 p.nextToken(); | |
639 members = p.parseModule(); | |
640 md = p.md; | |
641 numlines = p.loc.linnum; | |
642 | |
643 DsymbolTable dst; | |
644 | |
645 if (md !is null) | |
646 { | |
647 this.ident = md.id; | |
648 this.safe = md.safe; | |
649 dst = super.resolve(md.packages, &this.parent, null); | |
650 } | |
651 else | |
652 { | |
653 dst = modules; | |
654 | |
655 /* Check to see if module name is a valid identifier | |
656 */ | |
657 if (!Lexer.isValidIdentifier(this.ident.toChars())) | |
658 error("has non-identifier characters in filename, use module declaration instead"); | |
659 } | |
660 | |
661 // Update global list of modules | |
662 if (!dst.insert(this)) | |
663 { | |
664 if (md) | |
665 error(loc, "is in multiple packages %s", md.toChars()); | |
666 else | |
667 error(loc, "is in multiple defined"); | |
668 } | |
669 else | |
670 { | |
671 amodules.push(cast(void*)this); | |
672 } | |
673 } | |
674 } | |
675 | |
676 void semantic() // semantic analysis | |
677 { | |
678 int i; | |
679 | |
680 if (semanticstarted) | |
681 return; | |
682 | |
683 //printf("+Module.semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent); | |
684 semanticstarted = 1; | |
685 | |
686 // Note that modules get their own scope, from scratch. | |
687 // This is so regardless of where in the syntax a module | |
688 // gets imported, it is unaffected by context. | |
689 Scope sc = Scope.createGlobal(this); // create root scope | |
690 | |
691 //printf("Module = %p, linkage = %d\n", sc.scopesym, sc.linkage); | |
692 | |
693 // Add import of "object" if this module isn't "object" | |
694 if (ident !is Id.object) | |
695 { | |
696 Import im = new Import(Loc(0), null, Id.object, null, 0); | |
697 members.shift(cast(void*)im); | |
698 } | |
699 | |
700 // Add all symbols into module's symbol table | |
701 symtab = new DsymbolTable(); | |
702 for (i = 0; i < members.dim; i++) | |
703 { | |
704 Dsymbol s = cast(Dsymbol)members.data[i]; | |
13 | 705 s.addMember(null, sc.scopesym, true); |
0 | 706 } |
707 | |
708 /* Set scope for the symbols so that if we forward reference | |
709 * a symbol, it can possibly be resolved on the spot. | |
710 * If this works out well, it can be extended to all modules | |
711 * before any semantic() on any of them. | |
712 */ | |
713 for (i = 0; i < members.dim; i++) | |
714 { | |
715 Dsymbol s = cast(Dsymbol)members.data[i]; | |
716 s.setScope(sc); | |
717 } | |
718 | |
719 // Pass 1 semantic routines: do public side of the definition | |
720 for (i = 0; i < members.dim; i++) | |
721 { | |
722 Dsymbol s = cast(Dsymbol)members.data[i]; | |
723 | |
724 //writef("\tModule('%s'): '%s'.semantic()\n", toChars(), s.toChars()); | |
725 s.semantic(sc); | |
726 runDeferredSemantic(); | |
727 } | |
728 | |
729 sc = sc.pop(); | |
730 sc.pop(); // 2 pops because Scope.createGlobal() created 2 | |
731 semanticRun = semanticstarted; | |
732 //printf("-Module.semantic(this = %p, '%s'): parent = %p\n", this, toChars(), parent); | |
733 } | |
734 | |
735 void semantic2() // pass 2 semantic analysis | |
736 { | |
737 if (deferred.dim) | |
738 { | |
739 for (int i = 0; i < deferred.dim; i++) | |
740 { | |
741 Dsymbol sd = cast(Dsymbol)deferred.data[i]; | |
742 | |
743 sd.error("unable to resolve forward reference in definition"); | |
744 } | |
745 return; | |
746 } | |
747 //printf("Module.semantic2('%s'): parent = %p\n", toChars(), parent); | |
748 if (semanticstarted >= 2) | |
749 return; | |
750 assert(semanticstarted == 1); | |
751 semanticstarted = 2; | |
752 | |
753 // Note that modules get their own scope, from scratch. | |
754 // This is so regardless of where in the syntax a module | |
755 // gets imported, it is unaffected by context. | |
756 Scope sc = Scope.createGlobal(this); // create root scope | |
757 //printf("Module = %p\n", sc.scopesym); | |
758 | |
759 // Pass 2 semantic routines: do initializers and function bodies | |
760 for (int i = 0; i < members.dim; i++) | |
761 { | |
762 Dsymbol s = cast(Dsymbol)members.data[i]; | |
763 s.semantic2(sc); | |
764 } | |
765 | |
766 sc = sc.pop(); | |
767 sc.pop(); | |
768 semanticRun = semanticstarted; | |
769 //printf("-Module.semantic2('%s'): parent = %p\n", toChars(), parent); | |
770 } | |
771 | |
772 void semantic3() // pass 3 semantic analysis | |
773 { | |
774 //printf("Module.semantic3('%s'): parent = %p\n", toChars(), parent); | |
775 if (semanticstarted >= 3) | |
776 return; | |
777 assert(semanticstarted == 2); | |
778 semanticstarted = 3; | |
779 | |
780 // Note that modules get their own scope, from scratch. | |
781 // This is so regardless of where in the syntax a module | |
782 // gets imported, it is unaffected by context. | |
783 Scope sc = Scope.createGlobal(this); // create root scope | |
784 //printf("Module = %p\n", sc.scopesym); | |
785 | |
786 // Pass 3 semantic routines: do initializers and function bodies | |
787 for (int i = 0; i < members.dim; i++) | |
788 { | |
789 Dsymbol s = cast(Dsymbol)members.data[i]; | |
790 //printf("Module %s: %s.semantic3()\n", toChars(), s.toChars()); | |
791 s.semantic3(sc); | |
792 } | |
793 | |
794 sc = sc.pop(); | |
795 sc.pop(); | |
796 semanticRun = semanticstarted; | |
797 } | |
798 | |
72 | 799 override void inlineScan() // scan for functions to inline |
0 | 800 { |
801 int i; | |
802 | |
803 if (semanticstarted >= 4) | |
804 return; | |
805 | |
806 assert(semanticstarted == 3); | |
807 semanticstarted = 4; | |
808 | |
809 // Note that modules get their own scope, from scratch. | |
810 // This is so regardless of where in the syntax a module | |
811 // gets imported, it is unaffected by context. | |
812 //printf("Module = %p\n", sc.scopesym); | |
813 | |
814 for (i = 0; i < members.dim; i++) | |
815 { | |
816 Dsymbol s = cast(Dsymbol)members.data[i]; | |
817 //if (global.params.verbose) | |
818 //printf("inline scan symbol %s\n", s.toChars()); | |
819 s.inlineScan(); | |
820 } | |
821 | |
822 semanticRun = semanticstarted; | |
823 } | |
824 | |
825 void setHdrfile() // set hdrfile member | |
826 { | |
827 FileName hdrfilename; | |
828 string arghdr; | |
829 | |
830 if (global.params.hdrname) | |
831 arghdr = global.params.hdrname; | |
832 else if (global.params.preservePaths) | |
833 arghdr = arg; | |
834 else | |
835 arghdr = FileName.name(arg); | |
836 if (!FileName.absolute(arghdr)) | |
837 { | |
838 //FileName.ensurePathExists(global.params.hdrdir); | |
839 arghdr = FileName.combine(global.params.hdrdir, arghdr); | |
840 } | |
841 if (global.params.hdrname) | |
842 hdrfilename = new FileName(arghdr); | |
843 else | |
844 hdrfilename = FileName.forceExt(arghdr, global.hdr_ext); | |
845 | |
846 if (hdrfilename.str == srcfile.name.str) | |
847 { | |
848 error("Source file and 'header' file have same name '%s'", srcfile.name.str); | |
849 fatal(); | |
850 } | |
851 | |
852 hdrfile = new File(hdrfilename); | |
853 } | |
854 | |
855 version (_DH) { | |
856 void genhdrfile() // generate D import file | |
857 { | |
858 assert(false); | |
859 } | |
860 } | |
861 | |
862 /************************************** | |
863 * Generate .obj file for Module. | |
864 */ | |
865 void genobjfile(int multiobj) | |
866 { | |
867 //EEcontext *ee = env.getEEcontext(); | |
868 | |
869 //printf("Module.genobjfile(multiobj = %d) %s\n", multiobj, toChars()); | |
870 | |
871 lastmname = srcfile.toChars(); /// global mutation | |
872 | |
873 obj_initfile(toStringz(lastmname), null, toStringz(toPrettyChars())); | |
874 | |
875 eictor = null; | |
876 ictorlocalgot = null; | |
877 ector = null; | |
878 ectorgates.setDim(0); | |
879 edtor = null; | |
880 etest = null; | |
881 dtorcount = 0; | |
882 | |
883 if (doppelganger) | |
884 { | |
885 /* Generate a reference to the moduleinfo, so the module constructors | |
886 * and destructors get linked in. | |
887 */ | |
888 Module m = cast(Module)aimports.data[0]; | |
889 assert(m); | |
890 if (m.sictor || m.sctor || m.sdtor) | |
891 { | |
892 Symbol* s = m.toSymbol(); | |
893 //objextern(s); | |
894 //if (!s.Sxtrnnum) objextdef(s.Sident); | |
895 if (!s.Sxtrnnum) | |
896 { | |
897 //printf("%s\n", s.Sident); | |
898 static if (false) { | |
899 /* This should work, but causes optlink to fail in common/newlib.asm */ | |
900 objextdef(s.Sident); | |
901 } else { | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
902 version (ELFOBJ_OR_MACHOBJ) {///ELFOBJ || MACHOBJ |
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
903 int nbytes = reftoident(Segment.DATA, Offset(Segment.DATA), s, 0, CF.CFoff); |
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
904 Offset(Segment.DATA) += nbytes; |
0 | 905 } else { |
906 int nbytes = reftoident(Segment.DATA, Doffset, s, 0, CF.CFoff); | |
907 Doffset() += nbytes; | |
908 } | |
909 } | |
910 } | |
911 } | |
912 } | |
913 | |
914 if (global.params.cov) | |
915 { | |
916 /* Create coverage identifier: | |
917 * private uint[numlines] __coverage; | |
918 */ | |
919 cov = symbol_calloc("__coverage"); | |
920 cov.Stype = type_fake(TYM.TYint); | |
921 cov.Stype.Tmangle = mTYman.mTYman_c; | |
922 cov.Stype.Tcount++; | |
923 cov.Sclass = SC.SCstatic; | |
924 cov.Sfl = FL.FLdata; | |
925 version (ELFOBJ_OR_MACHOBJ) { | |
926 cov.Sseg = Segment.UDATA; | |
927 } | |
928 dtnzeros(&cov.Sdt, 4 * numlines); | |
929 outdata(cov); | |
930 slist_add(cov); | |
931 | |
4 | 932 covb = cast(uint*)GC.calloc(((numlines + 32) / 32) * (*covb).sizeof); |
0 | 933 } |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
934 |
0 | 935 for (int i = 0; i < members.dim; i++) |
936 { | |
937 Dsymbol member = cast(Dsymbol)members.data[i]; | |
938 member.toObjFile(multiobj); | |
939 } | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
940 |
0 | 941 if (global.params.cov) |
942 { | |
943 /* Generate | |
944 * bit[numlines] __bcoverage; | |
945 */ | |
946 Symbol* bcov = symbol_calloc("__bcoverage"); | |
947 bcov.Stype = type_fake(TYM.TYuint); | |
948 bcov.Stype.Tcount++; | |
949 bcov.Sclass = SC.SCstatic; | |
950 bcov.Sfl = FL.FLdata; | |
951 version (ELFOBJ_OR_MACHOBJ) { | |
952 bcov.Sseg = Segment.DATA; | |
953 } | |
954 dtnbytes(&bcov.Sdt, (numlines + 32) / 32 * (*covb).sizeof, cast(char*)covb); | |
955 outdata(bcov); | |
956 | |
2 | 957 ///free(covb); |
0 | 958 covb = null; |
959 | |
960 /* Generate: | |
961 * _d_cover_register(uint[] __coverage, BitArray __bcoverage, string filename); | |
962 * and prepend it to the static constructor. | |
963 */ | |
964 | |
965 /* t will be the type of the functions generated: | |
966 * extern (C) void func(); | |
967 */ | |
968 type* t = type_alloc(TYM.TYnfunc); | |
969 t.Tflags |= TF.TFprototype | TF.TFfixed; | |
970 t.Tmangle = mTYman.mTYman_c; | |
971 t.Tnext = tsvoid; | |
972 tsvoid.Tcount++; | |
973 | |
974 sictor = toSymbolX("__modictor", SC.SCglobal, t, "FZv"); | |
975 cstate.CSpsymtab = &sictor.Sfunc.Flocsym; | |
976 localgot = ictorlocalgot; | |
977 elem* e; | |
978 | |
979 e = el_params(el_ptr(cov), el_long(TYM.TYuint, numlines), | |
980 el_ptr(bcov), el_long(TYM.TYuint, numlines), | |
981 toEfilename(), | |
982 null); | |
983 e = el_bin(OPER.OPcall, TYM.TYvoid, el_var(rtlsym[RTLSYM.RTLSYM_DCOVER]), e); | |
984 eictor = el_combine(e, eictor); | |
985 ictorlocalgot = localgot; | |
986 } | |
987 | |
988 // If coverage / static constructor / destructor / unittest calls | |
989 if (eictor || ector || ectorgates.dim || edtor || etest) | |
990 { | |
991 /* t will be the type of the functions generated: | |
992 * extern (C) void func(); | |
993 */ | |
994 type* t = type_alloc(TYM.TYnfunc); | |
995 t.Tflags |= TF.TFprototype | TF.TFfixed; | |
996 t.Tmangle = mTYman.mTYman_c; | |
997 t.Tnext = tsvoid; | |
998 tsvoid.Tcount++; | |
999 | |
1000 static string moddeco = "FZv"; | |
1001 | |
1002 if (eictor) | |
1003 { | |
1004 localgot = ictorlocalgot; | |
1005 | |
1006 block* b = block_calloc(); | |
1007 b.BC = BC.BCret; | |
1008 b.Belem = eictor; | |
1009 sictor.Sfunc.Fstartblock = b; | |
1010 writefunc(sictor); | |
1011 } | |
1012 | |
1013 if (ector || ectorgates.dim) | |
1014 { | |
1015 localgot = null; | |
1016 sctor = toSymbolX("__modctor", SC.SCglobal, t, moddeco); | |
1017 cstate.CSpsymtab = &sctor.Sfunc.Flocsym; | |
1018 | |
1019 for (int i = 0; i < ectorgates.dim; i++) | |
1020 { | |
1021 StaticDtorDeclaration f = cast(StaticDtorDeclaration)ectorgates.data[i]; | |
1022 | |
1023 Symbol* s = f.vgate.toSymbol(); | |
1024 elem* e = el_var(s); | |
1025 e = el_bin(OPER.OPaddass, TYM.TYint, e, el_long(TYM.TYint, 1)); | |
1026 ector = el_combine(ector, e); | |
1027 } | |
1028 | |
1029 block* b = block_calloc(); | |
1030 b.BC = BC.BCret; | |
1031 b.Belem = ector; | |
1032 sctor.Sfunc.Fstartblock = b; | |
1033 writefunc(sctor); | |
1034 version (STATICCTOR) { | |
1035 obj_staticctor(sctor, dtorcount, 1); | |
1036 } | |
1037 } | |
1038 | |
1039 if (edtor) | |
1040 { | |
1041 localgot = null; | |
1042 sdtor = toSymbolX("__moddtor", SC.SCglobal, t, moddeco); | |
1043 | |
1044 block* b = block_calloc(); | |
1045 b.BC = BC.BCret; | |
1046 b.Belem = edtor; | |
1047 sdtor.Sfunc.Fstartblock = b; | |
1048 writefunc(sdtor); | |
1049 } | |
1050 | |
1051 if (etest) | |
1052 { | |
1053 localgot = null; | |
1054 stest = toSymbolX("__modtest", SC.SCglobal, t, moddeco); | |
1055 | |
1056 block* b = block_calloc(); | |
1057 b.BC = BC.BCret; | |
1058 b.Belem = etest; | |
1059 stest.Sfunc.Fstartblock = b; | |
1060 writefunc(stest); | |
1061 } | |
1062 | |
1063 if (doppelganger) | |
1064 genmoduleinfo(); | |
1065 } | |
1066 | |
1067 if (doppelganger) | |
1068 { | |
1069 obj_termfile(); | |
1070 return; | |
1071 } | |
1072 | |
1073 if (global.params.multiobj) | |
1074 { /* This is necessary because the main .obj for this module is written | |
1075 * first, but determining whether marray or massert are needed is done | |
1076 * possibly later in the doppelganger modules. | |
1077 * Another way to fix it is do the main one last. | |
1078 */ | |
1079 toModuleAssert(); | |
1080 toModuleArray(); | |
1081 } | |
1082 | |
1083 // If module assert | |
1084 for (int i = 0; i < 2; i++) | |
1085 { | |
1086 Symbol* ma = i ? marray : massert; | |
1087 | |
1088 if (ma) | |
1089 { | |
1090 elem* elinnum; | |
1091 elem* efilename; | |
1092 | |
1093 localgot = null; | |
1094 | |
1095 // Call dassert(filename, line) | |
1096 // Get sole parameter, linnum | |
1097 { | |
1098 Symbol* sp; | |
1099 | |
1100 sp = symbol_calloc("linnum".ptr); | |
1101 sp.Stype = type_fake(TYM.TYint); | |
1102 sp.Stype.Tcount++; | |
1103 sp.Sclass = SC.SCfastpar; | |
1104 sp.Spreg = REG.AX; | |
1105 sp.Sflags &= ~SFL.SFLspill; | |
1106 sp.Sfl = FL.FLpara; // FLauto? | |
1107 cstate.CSpsymtab = &ma.Sfunc.Flocsym; | |
1108 symbol_add(sp); | |
1109 | |
1110 elinnum = el_var(sp); | |
1111 } | |
1112 | |
1113 efilename = toEmodulename(); | |
1114 | |
1115 elem *e = el_var(rtlsym[i ? RTLSYM.RTLSYM_DARRAY : RTLSYM.RTLSYM_DASSERT]); | |
1116 e = el_bin(OPER.OPcall, TYM.TYvoid, e, el_param(elinnum, efilename)); | |
1117 | |
1118 block* b = block_calloc(); | |
1119 b.BC = BC.BCret; | |
1120 b.Belem = e; | |
1121 ma.Sfunc.Fstartblock = b; | |
1122 ma.Sclass = SC.SCglobal; | |
1123 ma.Sfl = 0; | |
1124 writefunc(ma); | |
1125 } | |
1126 } | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
1127 |
0 | 1128 |
1129 static if (true) { | |
1130 // Always generate module info, because of templates and -cov | |
1131 if (1 || needModuleInfo()) | |
1132 genmoduleinfo(); | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
1133 } |
0 | 1134 |
1135 obj_termfile(); | |
1136 } | |
1137 | |
1138 void gensymfile() | |
1139 { | |
1140 assert(false); | |
1141 } | |
1142 | |
1143 void gendocfile() | |
1144 { | |
1145 assert(false); | |
1146 } | |
1147 | |
1148 /********************************** | |
1149 * Determine if we need to generate an instance of ModuleInfo | |
1150 * for this Module. | |
1151 */ | |
1152 bool needModuleInfo() | |
1153 { | |
1154 return needmoduleinfo || global.params.cov; | |
1155 } | |
1156 | |
72 | 1157 override Dsymbol search(Loc loc, Identifier ident, int flags) |
0 | 1158 { |
1159 /* Since modules can be circularly referenced, | |
1160 * need to stop infinite recursive searches. | |
1161 */ | |
1162 | |
1163 //printf("%s Module.search('%s', flags = %d) insearch = %d\n", toChars(), ident.toChars(), flags, insearch); | |
1164 Dsymbol s; | |
1165 if (insearch) | |
1166 s = null; | |
73 | 1167 else if (searchCacheIdent == ident && searchCacheFlags == flags && searchCacheSymbol) |
1168 { | |
0 | 1169 s = searchCacheSymbol; |
73 | 1170 //printf("%s Module.search('%s', flags = %d) insearch = %d searchCacheSymbol = %s\n", toChars(), ident.toChars(), flags, insearch, searchCacheSymbol ? searchCacheSymbol.toChars() : "null"); |
1171 } | |
0 | 1172 else |
1173 { | |
1174 insearch = 1; | |
1175 s = ScopeDsymbol.search(loc, ident, flags); | |
1176 insearch = 0; | |
1177 | |
1178 searchCacheIdent = ident; | |
1179 searchCacheSymbol = s; | |
1180 searchCacheFlags = flags; | |
1181 } | |
1182 return s; | |
1183 } | |
1184 | |
1185 void deleteObjFile() | |
1186 { | |
1187 if (global.params.obj) | |
1188 objfile.remove(); | |
1189 if (docfile) | |
1190 docfile.remove(); | |
1191 } | |
1192 | |
29
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1193 /******************************************* |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1194 * Can't run semantic on s now, try again later. |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1195 */ |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1196 void addDeferredSemantic(Dsymbol s) |
0 | 1197 { |
29
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1198 // Don't add it if it is already there |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1199 for (int i = 0; i < deferred.dim; i++) |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1200 { |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1201 Dsymbol sd = cast(Dsymbol)deferred.data[i]; |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1202 |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1203 if (sd == s) |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1204 return; |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1205 } |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1206 |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1207 //printf("Module::addDeferredSemantic('%s')\n", s->toChars()); |
9fe59705c82d
Implemented Module.addDeferredSemantic, removed -release from the linux commands file.
Robert Clipsham <robert@octarineparrot.com>
parents:
23
diff
changeset
|
1208 deferred.push(cast(void*)s); |
0 | 1209 } |
1210 | |
1211 /****************************************** | |
1212 * Run semantic() on deferred symbols. | |
1213 */ | |
1214 | |
1215 void runDeferredSemantic() | |
1216 { | |
1217 size_t len; | |
1218 | |
1219 static int nested; | |
1220 if (nested) | |
1221 return; | |
1222 //if (deferred.dim) printf("+Module.runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim); | |
1223 nested++; | |
1224 | |
1225 do | |
1226 { | |
1227 dprogress = 0; | |
1228 len = deferred.dim; | |
1229 if (!len) | |
1230 break; | |
1231 | |
1232 Dsymbol *todo; | |
1233 Dsymbol tmp; | |
1234 if (len == 1) | |
1235 { | |
1236 todo = &tmp; | |
1237 } | |
1238 else | |
1239 { | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
1240 version(Bug4054) |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
1241 todo = cast(Dsymbol*)GC.malloc(len * (Dsymbol*).sizeof); |
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
1242 else |
0 | 1243 todo = cast(Dsymbol*)alloca(len * (Dsymbol*).sizeof); |
1244 assert(todo); | |
1245 } | |
1246 memcpy(todo, deferred.data, len * (Dsymbol*).sizeof); | |
1247 deferred.setDim(0); | |
1248 | |
1249 for (int i = 0; i < len; i++) | |
1250 { | |
1251 Dsymbol s = todo[i]; | |
1252 | |
1253 s.semantic(null); | |
1254 //printf("deferred: %s, parent = %s\n", s.toChars(), s.parent.toChars()); | |
1255 } | |
1256 //printf("\tdeferred.dim = %d, len = %d, dprogress = %d\n", deferred.dim, len, dprogress); | |
1257 } while (deferred.dim < len || dprogress); // while making progress | |
1258 nested--; | |
1259 //printf("-Module.runDeferredSemantic('%s'), len = %d\n", toChars(), deferred.dim); | |
1260 } | |
1261 | |
1262 int imports(Module m) | |
1263 { | |
1264 assert(false); | |
1265 } | |
1266 | |
1267 // Back end | |
1268 | |
1269 int doppelganger; // sub-module | |
1270 Symbol* cov; // private uint[] __coverage; | |
1271 uint* covb; // bit array of valid code line numbers | |
1272 | |
1273 Symbol* sictor; // module order independent constructor | |
1274 Symbol* sctor; // module constructor | |
1275 Symbol* sdtor; // module destructor | |
1276 Symbol* stest; // module unit test | |
1277 | |
1278 Symbol* sfilename; // symbol for filename | |
1279 | |
1280 Symbol* massert; // module assert function | |
1281 Symbol* toModuleAssert() // get module assert function | |
1282 { | |
1283 if (!massert) | |
1284 { | |
1285 type* t; | |
1286 | |
1287 t = type_alloc(TYjfunc); | |
1288 t.Tflags |= TFprototype | TFfixed; | |
1289 t.Tmangle = mTYman_d; | |
1290 t.Tnext = tsvoid; | |
1291 tsvoid.Tcount++; | |
1292 | |
1293 massert = toSymbolX("__assert", SCextern, t, "FiZv"); | |
1294 massert.Sfl = FLextern; | |
1295 massert.Sflags |= SFLnodebug; | |
1296 slist_add(massert); | |
1297 } | |
1298 return massert; | |
1299 } | |
1300 | |
1301 Symbol* marray; // module array bounds function | |
1302 | |
1303 Symbol* toModuleArray() // get module array bounds function | |
1304 { | |
1305 if (!marray) | |
1306 { | |
1307 type* t; | |
1308 | |
1309 t = type_alloc(TYjfunc); | |
1310 t.Tflags |= TFprototype | TFfixed; | |
1311 t.Tmangle = mTYman_d; | |
1312 t.Tnext = tsvoid; | |
1313 tsvoid.Tcount++; | |
1314 | |
1315 marray = toSymbolX("__array", SCextern, t, "Z"); | |
1316 marray.Sfl = FLextern; | |
1317 marray.Sflags |= SFLnodebug; | |
1318 slist_add(marray); | |
1319 } | |
1320 return marray; | |
1321 } | |
1322 | |
1323 static Symbol* gencritsec() | |
1324 { | |
1325 assert(false); | |
1326 } | |
1327 | |
1328 elem* toEfilename() | |
1329 { | |
1330 elem* efilename; | |
1331 | |
1332 if (!sfilename) | |
1333 { | |
1334 dt_t* dt = null; | |
1335 | |
1336 string id = srcfile.toChars(); | |
1337 int len = id.length; | |
1338 dtdword(&dt, len); | |
1339 dtabytes(&dt,TYnptr, 0, len + 1, toStringz(id)); | |
1340 | |
1341 sfilename = symbol_generate(SCstatic,type_fake(TYdarray)); | |
1342 sfilename.Sdt = dt; | |
1343 sfilename.Sfl = FLdata; | |
1344 version (ELFOBJ) { | |
1345 sfilename.Sseg = Segment.CDATA; | |
1346 } | |
1347 version (MACHOBJ) { | |
1348 // Because of PIC and CDATA being in the _TEXT segment, cannot | |
1349 // have pointers in CDATA | |
1350 sfilename.Sseg = Segment.DATA; | |
1351 } | |
1352 outdata(sfilename); | |
1353 } | |
1354 | |
1355 efilename = el_var(sfilename); | |
1356 return efilename; | |
1357 } | |
1358 | |
1359 /************************************** | |
1360 * Generate elem that is a pointer to the module file name. | |
1361 */ | |
1362 elem* toEmodulename() | |
1363 { | |
1364 elem *efilename; | |
1365 | |
1366 // Get filename | |
1367 if (needModuleInfo()) | |
1368 { | |
1369 /* Class ModuleInfo is defined in std.moduleinfo. | |
1370 * The first member is the name of it, char name[], | |
1371 * which will be at offset 8. | |
1372 */ | |
1373 | |
1374 Symbol* si = toSymbol(); | |
1375 static if (true) { | |
1376 // Use this instead so -fPIC will work | |
1377 efilename = el_ptr(si); | |
1378 efilename = el_bin(OPadd, TYnptr, efilename, el_long(TYuint, 8)); | |
1379 efilename = el_una(OPind, TYdarray, efilename); | |
1380 } else { | |
1381 efilename = el_var(si); | |
1382 efilename.Ety = TYdarray; | |
1383 efilename.EV.sp.Voffset += 8; | |
1384 } | |
1385 } | |
1386 else // generate our own filename | |
1387 { | |
1388 efilename = toEfilename(); | |
1389 } | |
1390 return efilename; | |
1391 } | |
1392 | |
1393 /************************************* | |
1394 * Create the "ModuleInfo" symbol | |
1395 */ | |
72 | 1396 override Symbol* toSymbol() |
0 | 1397 { |
1398 if (!csym) | |
1399 { | |
1400 Symbol* s; | |
1401 static Classsym* scc; | |
1402 | |
1403 if (!scc) { | |
1404 scc = fake_classsym(Id.ClassInfo); | |
1405 } | |
1406 | |
1407 s = toSymbolX("__ModuleInfo", SC.SCextern, scc.Stype, "Z"); | |
1408 s.Sfl = FL.FLextern; | |
1409 s.Sflags |= SFL.SFLnodebug; | |
1410 csym = s; | |
1411 slist_add(s); | |
1412 } | |
1413 return csym; | |
1414 } | |
1415 | |
1416 // Put out instance of ModuleInfo for this Module | |
1417 void genmoduleinfo() | |
1418 { | |
1419 //printf("Module.genmoduleinfo() %s\n", toChars()); | |
1420 | |
1421 Symbol* msym = toSymbol(); | |
1422 | |
1423 //dumpSymbol(msym); | |
1424 | |
1425 uint offset; | |
1426 version (DMDV2) { | |
1427 uint sizeof_ModuleInfo = 18 * PTRSIZE; | |
1428 } else { | |
1429 uint sizeof_ModuleInfo = 14 * PTRSIZE; | |
1430 } | |
1431 | |
1432 ////////////////////////////////////////////// | |
1433 | |
1434 csym.Sclass = SC.SCglobal; | |
1435 | |
1436 csym.Sfl = FL.FLdata; | |
1437 | |
1438 /* The layout is: | |
1439 { | |
1440 void **vptr; | |
1441 monitor_t monitor; | |
1442 char[] name; // class name | |
1443 ModuleInfo importedModules[]; | |
1444 ClassInfo localClasses[]; | |
1445 uint flags; // initialization state | |
1446 void *ctor; | |
1447 void *dtor; | |
1448 void *unitTest; | |
1449 const(MemberInfo[]) function(string) xgetMembers; // module getMembers() function | |
1450 void *ictor; | |
1451 void*[4] reserved; | |
1452 } | |
1453 */ | |
1454 dt_t* dt = null; | |
1455 | |
1456 if (moduleinfo) | |
1457 dtxoff(&dt, moduleinfo.toVtblSymbol(), 0, TYM.TYnptr); // vtbl for ModuleInfo | |
1458 else | |
1459 { | |
1460 //printf("moduleinfo is null\n"); | |
1461 dtdword(&dt, 0); // BUG: should be an assert() | |
1462 } | |
1463 dtdword(&dt, 0); // monitor | |
1464 | |
1465 // name[] | |
1466 string name = toPrettyChars(); | |
1467 size_t namelen = name.length; | |
1468 dtdword(&dt, namelen); | |
1469 dtabytes(&dt, TYM.TYnptr, 0, namelen + 1, toStringz(name)); | |
1470 | |
1471 ClassDeclarations aclasses = new ClassDeclarations(); | |
1472 | |
1473 //printf("members.dim = %d\n", members.dim); | |
1474 for (int i = 0; i < members.dim; i++) | |
1475 { | |
1476 Dsymbol member = cast(Dsymbol)members.data[i]; | |
1477 | |
1478 //printf("\tmember '%s'\n", member.toChars()); | |
1479 member.addLocalClass(aclasses); | |
1480 } | |
1481 | |
1482 // importedModules[] | |
1483 int aimports_dim = aimports.dim; | |
1484 for (int i = 0; i < aimports.dim; i++) | |
1485 { | |
1486 Module m = cast(Module)aimports.data[i]; | |
1487 if (!m.needModuleInfo()) | |
1488 aimports_dim--; | |
1489 } | |
1490 | |
1491 dtdword(&dt, aimports_dim); | |
1492 if (aimports_dim) | |
1493 dtxoff(&dt, csym, sizeof_ModuleInfo, TYM.TYnptr); | |
1494 else | |
1495 dtdword(&dt, 0); | |
1496 | |
1497 // localClasses[] | |
1498 dtdword(&dt, aclasses.dim); | |
1499 if (aclasses.dim) | |
1500 dtxoff(&dt, csym, sizeof_ModuleInfo + aimports_dim * PTRSIZE, TYM.TYnptr); | |
1501 else | |
1502 dtdword(&dt, 0); | |
1503 | |
1504 if (needmoduleinfo) | |
1505 dtdword(&dt, 8|0); // flags (4 means MIstandalone) | |
1506 else | |
1507 dtdword(&dt, 8|4); // flags (4 means MIstandalone) | |
1508 | |
1509 if (sctor) | |
1510 dtxoff(&dt, sctor, 0, TYM.TYnptr); | |
1511 else | |
1512 dtdword(&dt, 0); | |
1513 | |
1514 if (sdtor) | |
1515 dtxoff(&dt, sdtor, 0, TYM.TYnptr); | |
1516 else | |
1517 dtdword(&dt, 0); | |
1518 | |
1519 if (stest) | |
1520 dtxoff(&dt, stest, 0, TYM.TYnptr); | |
1521 else | |
1522 dtdword(&dt, 0); | |
1523 | |
1524 /// version (DMDV2) { | |
1525 FuncDeclaration sgetmembers = findGetMembers(); | |
1526 if (sgetmembers) | |
1527 dtxoff(&dt, sgetmembers.toSymbol(), 0, TYM.TYnptr); | |
1528 else | |
1529 /// } | |
1530 dtdword(&dt, 0); // xgetMembers | |
1531 | |
1532 if (sictor) | |
1533 dtxoff(&dt, sictor, 0, TYM.TYnptr); | |
1534 else | |
1535 dtdword(&dt, 0); | |
1536 | |
1537 version (DMDV2) { | |
1538 // void*[4] reserved; | |
1539 dtdword(&dt, 0); | |
1540 dtdword(&dt, 0); | |
1541 dtdword(&dt, 0); | |
1542 dtdword(&dt, 0); | |
1543 } | |
1544 ////////////////////////////////////////////// | |
1545 | |
1546 for (int i = 0; i < aimports.dim; i++) | |
1547 { | |
1548 Module m = cast(Module)aimports.data[i]; | |
1549 | |
1550 if (m.needModuleInfo()) | |
1551 { | |
1552 Symbol* s = m.toSymbol(); | |
1553 | |
1554 /* Weak references don't pull objects in from the library, | |
1555 * they resolve to 0 if not pulled in by something else. | |
1556 * Don't pull in a module just because it was imported. | |
1557 */ | |
1558 version (OMFOBJ) {// Optlink crashes with weak symbols at EIP 41AFE7, 402000 | |
1559 } else { | |
1560 s.Sflags |= SFL.SFLweak; | |
1561 } | |
1562 dtxoff(&dt, s, 0, TYM.TYnptr); | |
1563 } | |
1564 } | |
1565 | |
1566 for (int i = 0; i < aclasses.dim; i++) | |
1567 { | |
1568 ClassDeclaration cd = cast(ClassDeclaration)aclasses.data[i]; | |
1569 dtxoff(&dt, cd.toSymbol(), 0, TYM.TYnptr); | |
1570 } | |
1571 | |
1572 csym.Sdt = dt; | |
1573 version (ELFOBJ_OR_MACHOBJ) { | |
1574 // Cannot be CONST because the startup code sets flag bits in it | |
1575 csym.Sseg = Segment.DATA; | |
1576 } | |
1577 | |
1578 outdata(csym); | |
1579 | |
1580 ////////////////////////////////////////////// | |
1581 | |
1582 obj_moduleinfo(msym); | |
1583 } | |
1584 | |
72 | 1585 override Module isModule() { return this; } |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
5
diff
changeset
|
1586 } |