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