0
|
1 module dmd.backend.glue;
|
|
2
|
|
3 import dmd.Array;
|
|
4 import dmd.Dsymbol;
|
|
5 import dmd.File;
|
|
6 import dmd.FileName;
|
|
7 import dmd.Library;
|
|
8 import dmd.OutBuffer;
|
|
9 import dmd.Module;
|
|
10 import dmd.Identifier;
|
|
11 import dmd.AssertExp;
|
|
12 import dmd.TOK;
|
|
13 import dmd.Global;
|
|
14 import dmd.Param;
|
|
15 import dmd.backend.Config;
|
|
16 import dmd.backend.Configv;
|
|
17 import dmd.backend.StringTab;
|
|
18
|
|
19 import core.stdc.string;
|
|
20
|
|
21 __gshared Array obj_symbols_towrite;
|
|
22
|
|
23 extern (C++) extern
|
|
24 {
|
|
25 __gshared Outbuffer objbuf;
|
|
26 int go_flag(char* cp);
|
|
27 void util_set64();
|
|
28 void util_set386();
|
|
29 }
|
|
30
|
|
31 import std.contracts;
|
|
32 import std.string;
|
|
33
|
|
34 string lastmname;
|
|
35
|
|
36 struct Outbuffer
|
|
37 {
|
|
38 ubyte* buf; // the buffer itself
|
|
39 ubyte* pend; // pointer past the end of the buffer
|
|
40 ubyte* p; // current position in buffer
|
|
41 uint len; // size of buffer
|
|
42 uint inc; // default increment size
|
|
43
|
|
44 this(uint inc)
|
|
45 {
|
|
46 assert(false);
|
|
47 }
|
|
48
|
|
49 ~this()
|
|
50 {
|
|
51 assert(false);
|
|
52 }
|
|
53
|
|
54 void reset()
|
|
55 {
|
|
56 assert(false);
|
|
57 }
|
|
58
|
|
59 // Reserve nbytes in buffer
|
|
60 void reserve(uint nbytes)
|
|
61 {
|
|
62 assert(false);
|
|
63 }
|
|
64
|
|
65 // Write n zeros; return pointer to start of zeros
|
|
66 void* writezeros(uint n)
|
|
67 {
|
|
68 assert(false);
|
|
69 }
|
|
70
|
|
71 // Position buffer to accept the specified number of bytes at offset
|
|
72 int position(uint offset, uint nbytes);
|
|
73
|
|
74 // Write an array to the buffer, no reserve check
|
|
75 void writen(const(void)* b, int len)
|
|
76 {
|
|
77 memcpy(p,b,len);
|
|
78 p += len;
|
|
79 }
|
|
80
|
|
81 // Clear bytes, no reserve check
|
|
82 void clearn(int len)
|
|
83 {
|
|
84 int i;
|
|
85 for (i=0; i< len; i++)
|
|
86 *p++ = 0;
|
|
87 }
|
|
88
|
|
89 // Write an array to the buffer.
|
|
90 void write(const void *b, int len)
|
|
91 {
|
|
92 assert(false);
|
|
93 }
|
|
94
|
|
95 void write(Outbuffer* b)
|
|
96 {
|
|
97 write(b.buf, b.p - b.buf);
|
|
98 }
|
|
99
|
|
100 /**
|
|
101 * Flushes the stream. This will write any buffered
|
|
102 * output bytes.
|
|
103 */
|
|
104 void flush() { }
|
|
105
|
|
106 /**
|
|
107 * Writes an 8 bit byte, no reserve check.
|
|
108 */
|
|
109 void writeByten(char v)
|
|
110 {
|
|
111 *p++ = v;
|
|
112 }
|
|
113
|
|
114 /**
|
|
115 * Writes an 8 bit byte.
|
|
116 */
|
|
117 void writeByte(int v)
|
|
118 {
|
|
119 assert(false);
|
|
120 }
|
|
121
|
|
122 /**
|
|
123 * Writes a 16 bit little-end short, no reserve check.
|
|
124 */
|
|
125 void writeWordn(int v)
|
|
126 {
|
|
127 version (_WIN32) {
|
|
128 *cast(ushort*)p = cast(short)v;
|
|
129 } else {
|
|
130 p[0] = v;
|
|
131 p[1] = v >> 8;
|
|
132 }
|
|
133 p += 2;
|
|
134 }
|
|
135
|
|
136 /**
|
|
137 * Writes a 16 bit little-end short.
|
|
138 */
|
|
139 void writeWord(int v)
|
|
140 {
|
|
141 reserve(2);
|
|
142 writeWordn(v);
|
|
143 }
|
|
144
|
|
145 /**
|
|
146 * Writes a 16 bit big-end short.
|
|
147 */
|
|
148 void writeShort(int v)
|
|
149 {
|
|
150 if (pend - p < 2)
|
|
151 reserve(2);
|
|
152
|
|
153 static if (false) {
|
|
154 p[0] = (cast(ubyte*)&v)[1];
|
|
155 p[1] = v;
|
|
156 } else {
|
|
157 ubyte* q = p;
|
|
158 q[0] = cast(ubyte)(v >> 8);
|
|
159 q[1] = cast(ubyte)v;
|
|
160 }
|
|
161 p += 2;
|
|
162 }
|
|
163
|
|
164 /**
|
|
165 * Writes a 16 bit char.
|
|
166 */
|
|
167 void writeChar(int v)
|
|
168 {
|
|
169 writeShort(v);
|
|
170 }
|
|
171
|
|
172 /**
|
|
173 * Writes a 32 bit int.
|
|
174 */
|
|
175 void write32(long v)
|
|
176 {
|
|
177 assert(false);
|
|
178 }
|
|
179
|
|
180 /**
|
|
181 * Writes a 64 bit long.
|
|
182 */
|
|
183 ///#if __INTSIZE == 4
|
|
184 void write64(long v)
|
|
185 {
|
|
186 assert(false);
|
|
187 }
|
|
188 ///#endif
|
|
189
|
|
190 /**
|
|
191 * Writes a 32 bit float.
|
|
192 */
|
|
193 void writeFloat(float v)
|
|
194 {
|
|
195 assert(false);
|
|
196 }
|
|
197
|
|
198 /**
|
|
199 * Writes a 64 bit double.
|
|
200 */
|
|
201 void writeDouble(double v)
|
|
202 {
|
|
203 assert(false);
|
|
204 }
|
|
205
|
|
206 void write(const(char)* s)
|
|
207 {
|
|
208 assert(false);
|
|
209 }
|
|
210
|
|
211 void write(const(ubyte)* s)
|
|
212 {
|
|
213 assert(false);
|
|
214 }
|
|
215
|
|
216 void writeString(const(char)* s)
|
|
217 {
|
|
218 assert(false);
|
|
219 }
|
|
220
|
|
221 void prependBytes(const(char)* s)
|
|
222 {
|
|
223 assert(false);
|
|
224 }
|
|
225
|
|
226 void bracket(char c1, char c2)
|
|
227 {
|
|
228 assert(false);
|
|
229 }
|
|
230
|
|
231 /**
|
|
232 * Returns the number of bytes written.
|
|
233 */
|
|
234 int size()
|
|
235 {
|
|
236 return p - buf;
|
|
237 }
|
|
238
|
|
239 char* toString()
|
|
240 {
|
|
241 assert(false);
|
|
242 }
|
|
243
|
|
244 void setsize(uint size)
|
|
245 {
|
|
246 assert(false);
|
|
247 }
|
|
248
|
|
249 void writesLEB128(long value)
|
|
250 {
|
|
251 assert(false);
|
|
252 }
|
|
253
|
|
254 void writeuLEB128(uint value)
|
|
255 {
|
|
256 assert(false);
|
|
257 }
|
|
258 }
|
|
259
|
|
260 static this()
|
|
261 {
|
|
262 obj_symbols_towrite = new Array();
|
|
263 }
|
|
264
|
|
265 /**************************************
|
|
266 * Append s to list of object files to generate later.
|
|
267 */
|
|
268
|
|
269 void obj_append(Dsymbol s)
|
|
270 {
|
|
271 obj_symbols_towrite.push(cast(void*)s);
|
|
272 }
|
|
273
|
|
274 extern (C++) {
|
|
275 void backend_init();
|
|
276 void backend_term();
|
|
277 void obj_term();
|
|
278 void rtlsym_reset();
|
|
279 void slist_reset();
|
|
280 void el_reset();
|
|
281 void cg87_reset();
|
|
282 void out_reset();
|
|
283 void obj_init(Outbuffer* objbuf, const(char)* filename, const(char)* csegname);
|
|
284 }
|
|
285
|
|
286 void clearStringTab()
|
|
287 {
|
|
288 //printf("clearStringTab()\n");
|
|
289 memset(stringTab.ptr, 0, stringTab.sizeof);
|
|
290 stidx = 0;
|
|
291
|
|
292 assertexp_sfilename = null;
|
|
293 assertexp_name = null;
|
|
294 assertexp_mn = null;
|
|
295 }
|
|
296
|
|
297 void obj_start(char *srcfile)
|
|
298 {
|
|
299 //printf("obj_start()\n");
|
|
300
|
|
301 out_config_init();
|
|
302
|
|
303 rtlsym_reset();
|
|
304 slist_reset();
|
|
305 clearStringTab();
|
|
306
|
|
307 obj_init(&objbuf, srcfile, null);
|
|
308
|
|
309 el_reset();
|
|
310 cg87_reset();
|
|
311 out_reset();
|
|
312 }
|
|
313
|
|
314 void obj_end(Library library, File objfile)
|
|
315 {
|
|
316 obj_term();
|
|
317
|
|
318 if (library)
|
|
319 {
|
|
320 // Transfer image to library
|
|
321 library.addObject(objfile.name.toChars(), objbuf.buf, objbuf.p - objbuf.buf);
|
|
322 objbuf.buf = null;
|
|
323 }
|
|
324 else
|
|
325 {
|
|
326 // Transfer image to file
|
|
327 objfile.setbuffer(objbuf.buf, objbuf.p - objbuf.buf);
|
|
328 objbuf.buf = null;
|
|
329
|
|
330 string p = FileName.path(objfile.name.toChars());
|
|
331 FileName.ensurePathExists(p);
|
|
332 //mem.free(p);
|
|
333
|
|
334 //printf("write obj %s\n", objfile.name.toChars());
|
|
335 objfile.writev();
|
|
336 }
|
|
337
|
|
338 objbuf.pend = null;
|
|
339 objbuf.p = null;
|
|
340 objbuf.len = 0;
|
|
341 objbuf.inc = 0;
|
|
342 }
|
|
343
|
|
344 void obj_write_deferred(Library library)
|
|
345 {
|
|
346 for (int i = 0; i < obj_symbols_towrite.dim; i++)
|
|
347 {
|
|
348 Dsymbol s = cast(Dsymbol)obj_symbols_towrite.data[i];
|
|
349 Module m = s.getModule();
|
|
350
|
|
351 string mname;
|
|
352 if (m)
|
|
353 {
|
|
354 mname = m.srcfile.toChars();
|
|
355 lastmname = mname;
|
|
356 }
|
|
357 else
|
|
358 {
|
|
359 //mname = s->ident->toChars();
|
|
360 mname = lastmname;
|
|
361 assert(mname.length != 0);
|
|
362 }
|
|
363
|
|
364 obj_start(cast(char*)toStringz(mname));
|
|
365
|
|
366 static int count;
|
|
367 count++; // sequence for generating names
|
|
368
|
|
369 /* Create a module that's a doppelganger of m, with just
|
|
370 * enough to be able to create the moduleinfo.
|
|
371 */
|
|
372 OutBuffer idbuf = new OutBuffer();
|
|
373 idbuf.printf("%s.%d", m ? m.ident.toChars() : mname, count);
|
|
374 string idstr = idbuf.extractString();
|
|
375 idbuf.data = null;
|
|
376 Identifier id = new Identifier(idstr, TOK.TOKidentifier);
|
|
377
|
|
378 Module md = new Module(mname, id, 0, 0);
|
|
379 md.members = new Array();
|
|
380 md.members.push(cast(void*)s); // its only 'member' is s
|
|
381 if (m)
|
|
382 {
|
|
383 md.doppelganger = 1; // identify this module as doppelganger
|
|
384 md.md = m.md;
|
|
385 md.aimports.push(cast(void*)m); // it only 'imports' m
|
|
386 md.massert = m.massert;
|
|
387 md.marray = m.marray;
|
|
388 }
|
|
389
|
|
390 md.genobjfile(0);
|
|
391
|
|
392 /* Set object file name to be source name with sequence number,
|
|
393 * as mangled symbol names get way too long.
|
|
394 */
|
|
395 string fname = FileName.removeExt(mname);
|
|
396
|
|
397 OutBuffer namebuf = new OutBuffer();
|
|
398 uint hash = 0;
|
|
399 foreach (char c; s.toChars())
|
|
400 hash += c;
|
|
401
|
|
402 namebuf.printf("%s_%x_%x.%s", fname, count, hash, global.obj_ext);
|
|
403 fname = namebuf.extractString();
|
|
404
|
|
405 //printf("writing '%s'\n", fname);
|
|
406 File objfile = new File(fname);
|
|
407 obj_end(library, objfile);
|
|
408 }
|
|
409
|
|
410 obj_symbols_towrite.dim = 0;
|
|
411 }
|
|
412
|
|
413 /**************************************
|
|
414 * Initialize config variables.
|
|
415 */
|
|
416
|
|
417 void out_config_init()
|
|
418 {
|
|
419 Param* params = &global.params;
|
|
420
|
|
421 if (!config.target_cpu)
|
|
422 {
|
|
423 config.target_cpu = TARGET_PentiumPro;
|
|
424 config.target_scheduler = config.target_cpu;
|
|
425 }
|
|
426 config.fulltypes = CVNONE;
|
|
427 config.inline8087 = 1;
|
|
428 config.memmodel = 0;
|
|
429 config.flags |= CFGuchar; // make sure TYchar is unsigned
|
|
430 version (TARGET_WINDOS) {
|
|
431 if (params.isX86_64)
|
|
432 config.exe = EX_WIN64;
|
|
433 else
|
|
434 config.exe = EX_NT;
|
|
435
|
|
436 // Win32 eh
|
|
437 config.flags2 |= CFG2seh;
|
|
438
|
|
439 if (params.run)
|
|
440 config.wflags |= WFexe; // EXE file only optimizations
|
|
441 else if (params.link && !global.params.deffile)
|
|
442 config.wflags |= WFexe; // EXE file only optimizations
|
|
443 else if (params.exefile) // if writing out EXE file
|
|
444 {
|
|
445 size_t len = params.exefile.length;
|
|
446 if (len >= 4 && icmp(params.exefile[len-3..len], "exe") == 0)
|
|
447 config.wflags |= WFexe;
|
|
448 }
|
|
449 config.flags4 |= CFG4underscore;
|
|
450 }
|
|
451 version (TARGET_LINUX) {
|
|
452 if (params.isX86_64)
|
|
453 config.exe = EX_LINUX64;
|
|
454 else
|
|
455 config.exe = EX_LINUX;
|
|
456 config.flags |= CFGnoebp;
|
|
457 config.flags |= CFGalwaysframe;
|
|
458 if (params.pic)
|
|
459 config.flags3 |= CFG3pic;
|
|
460 }
|
|
461 version (TARGET_OSX) {
|
|
462 if (params.isX86_64)
|
|
463 config.exe = EX_OSX64;
|
|
464 else
|
|
465 config.exe = EX_OSX;
|
|
466 config.flags |= CFGnoebp;
|
|
467 config.flags |= CFGalwaysframe;
|
|
468 if (params.pic)
|
|
469 config.flags3 |= CFG3pic;
|
|
470 }
|
|
471 version (TARGET_FREEBSD) {
|
|
472 if (params.isX86_64)
|
|
473 config.exe = EX_FREEBSD64;
|
|
474 else
|
|
475 config.exe = EX_FREEBSD;
|
|
476 config.flags |= CFGnoebp;
|
|
477 config.flags |= CFGalwaysframe;
|
|
478 if (params.pic)
|
|
479 config.flags3 |= CFG3pic;
|
|
480 }
|
|
481 version (TARGET_SOLARIS) {
|
|
482 if (params.isX86_64)
|
|
483 config.exe = EX_SOLARIS64;
|
|
484 else
|
|
485 config.exe = EX_SOLARIS;
|
|
486 config.flags |= CFGnoebp;
|
|
487 config.flags |= CFGalwaysframe;
|
|
488 if (params.pic)
|
|
489 config.flags3 |= CFG3pic;
|
|
490 }
|
|
491 config.flags2 |= CFG2nodeflib; // no default library
|
|
492 config.flags3 |= CFG3eseqds;
|
|
493 static if (false) {
|
|
494 if (env.getEEcontext().EEcompile != 2)
|
|
495 config.flags4 |= CFG4allcomdat;
|
|
496 if (env.nochecks())
|
|
497 config.flags4 |= CFG4nochecks; // no runtime checking
|
|
498 } else version (TARGET_OSX) {
|
|
499 } else {
|
|
500 config.flags4 |= CFG4allcomdat;
|
|
501 }
|
|
502 if (params.trace)
|
|
503 config.flags |= CFGtrace; // turn on profiler
|
|
504 if (params.nofloat)
|
|
505 config.flags3 |= CFG3wkfloat;
|
|
506
|
|
507 configv.verbose = params.verbose;
|
|
508
|
|
509 if (params.optimize)
|
|
510 go_flag(cast(char*)"-o".ptr);
|
|
511
|
|
512 if (params.symdebug)
|
|
513 {
|
|
514 version (ELFOBJ_OR_MACHOBJ) {
|
|
515 configv.addlinenumbers = 1;
|
|
516 config.fulltypes = (params.symdebug == 1) ? CVDWARF_D : CVDWARF_C;
|
|
517 }
|
|
518 version (OMFOBJ) {
|
|
519 configv.addlinenumbers = 1;
|
|
520 config.fulltypes = CV4;
|
|
521 }
|
|
522 if (!params.optimize)
|
|
523 config.flags |= CFGalwaysframe;
|
|
524 }
|
|
525 else
|
|
526 {
|
|
527 configv.addlinenumbers = 0;
|
|
528 config.fulltypes = CVNONE;
|
|
529 //config.flags &= ~CFGalwaysframe;
|
|
530 }
|
|
531
|
|
532 if (params.isX86_64)
|
|
533 {
|
|
534 util_set64();
|
|
535 cod3_set64();
|
|
536 }
|
|
537 else
|
|
538 {
|
|
539 util_set386();
|
|
540 cod3_set386();
|
|
541 }
|
|
542
|
|
543 debug {
|
|
544 debugb = params.debugb;
|
|
545 debugc = params.debugc;
|
|
546 debugf = params.debugf;
|
|
547 debugr = params.debugr;
|
|
548 debugw = params.debugw;
|
|
549 debugx = params.debugx;
|
|
550 debugy = params.debugy;
|
|
551 }
|
|
552 } |