1 /**
2 * Configures and initializes the backend.
3 *
4 * Copyright: Copyright (C) 1999-2020 by The D Language Foundation, All Rights Reserved
5 * Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
6 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
7 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/dmsc.d, _dmsc.d)
8 * Documentation: https://dlang.org/phobos/dmd_dmsc.html
9 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/dmsc.d
10 */
11
12 module dmd.dmsc;
13
14 import core.stdc.stdio;
15 import core.stdc..string;
16 import core.stdc.stddef;
17
18 extern (C++):
19
20 import dmd.globals;
21 import dmd.dclass;
22 import dmd.dmodule;
23 import dmd.mtype;
24
25 import dmd.root.filename;
26
27 import dmd.backend.cc;
28 import dmd.backend.cdef;
29 import dmd.backend.global;
30 import dmd.backend.ty;
31 import dmd.backend.type;
32
33 extern (C) void out_config_init(
34 int model, // 32: 32 bit code
35 // 64: 64 bit code
36 // Windows: bit 0 set to generate MS-COFF instead of OMF
37 bool exe, // true: exe file
38 // false: dll or shared library (generate PIC code)
39 bool trace, // add profiling code
40 bool nofloat, // do not pull in floating point code
41 bool verbose, // verbose compile
42 bool optimize, // optimize code
43 int symdebug, // add symbolic debug information
44 // 1: D
45 // 2: fake it with C symbolic debug info
46 bool alwaysframe, // always create standard function frame
47 bool stackstomp, // add stack stomping code
48 ubyte avx, // use AVX instruction set (0, 1, 2)
49 PIC pic, // kind of position independent code
50 bool useModuleInfo, // implement ModuleInfo
51 bool useTypeInfo, // implement TypeInfo
52 bool useExceptions, // implement exception handling
53 string _version // Compiler version
54 );
55
56 void out_config_debug(
57 bool debugb,
58 bool debugc,
59 bool debugf,
60 bool debugr,
61 bool debugw,
62 bool debugx,
63 bool debugy
64 );
65
66 /**************************************
67 * Initialize config variables.
68 */
69
70 void backend_init()
71 {
72 //printf("out_config_init()\n");
73 Param *params = &global.params;
74
75 bool exe;
76 if (params.dll || params.pic != PIC.fixed)
77 {
78 }
79 else if (params.run)
80 exe = true; // EXE file only optimizations
81 else if (params.link && !params.deffile)
82 exe = true; // EXE file only optimizations
83 else if (params.exefile.length &&
84 params.exefile.length >= 4 &&
85 FileName.equals(FileName.ext(params.exefile), "exe"))
86 exe = true; // if writing out EXE file
87
88 out_config_init(
89 (params.is64bit ? 64 : 32) | (params.mscoff ? 1 : 0),
90 exe,
91 false, //params.trace,
92 params.nofloat,
93 params.verbose,
94 params.optimize,
95 params.symdebug,
96 params.alwaysframe,
97 params.stackstomp,
98 params.cpu >= CPU.avx2 ? 2 : params.cpu >= CPU.avx ? 1 : 0,
99 params.pic,
100 params.useModuleInfo && Module.moduleinfo,
101 params.useTypeInfo && Type.dtypeinfo,
102 params.useExceptions && ClassDeclaration.throwable,
103 global._version
104 );
105
106 debug
107 {
108 out_config_debug(
109 params.debugb,
110 params.debugc,
111 params.debugf,
112 params.debugr,
113 false,
114 params.debugx,
115 params.debugy
116 );
117 }
118 }
119
120
121 /***********************************
122 * Return aligned 'offset' if it is of size 'size'.
123 */
124
125 targ_size_t _align(targ_size_t size, targ_size_t offset)
126 {
127 switch (size)
128 {
129 case 1:
130 break;
131 case 2:
132 case 4:
133 case 8:
134 case 16:
135 case 32:
136 case 64:
137 offset = (offset + size - 1) & ~(size - 1);
138 break;
139 default:
140 if (size >= 16)
141 offset = (offset + 15) & ~15;
142 else
143 offset = (offset + _tysize[TYnptr] - 1) & ~(_tysize[TYnptr] - 1);
144 break;
145 }
146 return offset;
147 }
148
149
150 /*******************************
151 * Get size of ty
152 */
153
154 targ_size_t size(tym_t ty)
155 {
156 int sz = (tybasic(ty) == TYvoid) ? 1 : tysize(ty);
157 debug
158 {
159 if (sz == -1)
160 WRTYxx(ty);
161 }
162 assert(sz!= -1);
163 return sz;
164 }
165
166 /****************************
167 * Generate symbol of type ty at DATA:offset
168 */
169
170 Symbol *symboldata(targ_size_t offset,tym_t ty)
171 {
172 Symbol *s = symbol_generate(SClocstat, type_fake(ty));
173 s.Sfl = FLdata;
174 s.Soffset = offset;
175 s.Stype.Tmangle = mTYman_sys; // writes symbol unmodified in Obj::mangle
176 symbol_keep(s); // keep around
177 return s;
178 }
179
180 /**************************************
181 */
182
183 void backend_term()
184 {
185 }