1 /**
2  * Declarations for back end
3  *
4  * Compiler implementation of the
5  * $(LINK2 https://www.dlang.org, D programming language).
6  *
7  * Copyright:   Copyright (C) 1984-1998 by Symantec
8  *              Copyright (C) 2000-2023 by The D Language Foundation, All Rights Reserved
9  * Authors:     $(LINK2 https://www.digitalmars.com, Walter Bright)
10  * License:     $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
11  * Source:      $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/backend/global.d, backend/global.d)
12  */
13 module dmd.backend.global;
14 
15 // Online documentation: https://dlang.org/phobos/dmd_backend_global.html
16 
17 @nogc:
18 nothrow:
19 
20 import core.stdc.stdio;
21 import core.stdc.stdint;
22 
23 import dmd.backend.cdef;
24 import dmd.backend.cc;
25 import dmd.backend.cc : Symbol, block, Classsym, Blockx;
26 import dmd.backend.code_x86 : code;
27 import dmd.backend.code;
28 import dmd.backend.dlist;
29 import dmd.backend.el;
30 import dmd.backend.el : elem;
31 import dmd.backend.mem;
32 import dmd.backend.symtab;
33 import dmd.backend.type;
34 //import dmd.backend.obj;
35 
36 import dmd.backend.barray;
37 
38 nothrow:
39 @safe:
40 
41 // FIXME: backend can't import front end modules because missing -J flag
42 extern (C++) void error(const(char)* filename, uint linnum, uint charnum, const(char)* format, ...);
43 package extern (C++) void fatal();
44 
45 public import dmd.backend.eh : except_gentables;
46 import dmd.backend.var : _tysize;
47 import dmd.backend.ty : TYnptr, TYvoid, tybasic, tysize;
48 
49 /***********************************
50  * Returns: aligned `offset` if it is of size `size`.
51  */
52 targ_size_t _align(targ_size_t size, targ_size_t offset) @trusted
53 {
54     switch (size)
55     {
56         case 1:
57             break;
58         case 2:
59         case 4:
60         case 8:
61         case 16:
62         case 32:
63         case 64:
64             offset = (offset + size - 1) & ~(size - 1);
65             break;
66         default:
67             if (size >= 16)
68                 offset = (offset + 15) & ~15;
69             else
70                 offset = (offset + _tysize[TYnptr] - 1) & ~(_tysize[TYnptr] - 1);
71             break;
72     }
73     return offset;
74 }
75 
76 /*******************************
77  * Get size of ty
78  */
79 targ_size_t size(tym_t ty) @trusted
80 {
81     int sz = (tybasic(ty) == TYvoid) ? 1 : tysize(ty);
82     debug
83     {
84         if (sz == -1)
85             printf("ty: %s\n", tym_str(ty));
86     }
87     assert(sz!= -1);
88     return sz;
89 }
90 
91 /****************************
92  * Generate symbol of type ty at DATA:offset
93  */
94 Symbol *symboldata(targ_size_t offset, tym_t ty)
95 {
96     Symbol *s = symbol_generate(SC.locstat, type_fake(ty));
97     s.Sfl = FLdata;
98     s.Soffset = offset;
99     s.Stype.Tmangle = mTYman_sys; // writes symbol unmodified in Obj::mangle
100     symbol_keep(s);               // keep around
101     return s;
102 }
103 
104 /// Size of a register in bytes
105 int REGSIZE() @trusted { return _tysize[TYnptr]; }
106 
107 public import dmd.backend.var : debuga, debugb, debugc, debugd, debuge, debugf,
108     debugr, debugs, debugt, debugu, debugw, debugx, debugy;
109 
110 extern (D) uint mask(uint m) { return 1 << m; }
111 
112 public import dmd.backend.var : OPTIMIZER, PARSER, globsym, controlc_saw, pointertype, sytab;
113 public import dmd.backend.cg : fregsaved, localgot, tls_get_addr_sym;
114 public import dmd.backend.blockopt : startblock, dfo, curblock, block_last;
115 
116 __gshared Configv configv;                // non-ph part of configuration
117 
118 public import dmd.backend.ee : eecontext_convs;
119 public import dmd.backend.elem : exp2_copytotemp;
120 public import dmd.backend.util2 : err_exit, ispow2;
121 
122 void* util_malloc(uint n,uint size) { return mem_malloc(n * size); }
123 void* util_calloc(uint n,uint size) { return mem_calloc(n * size); }
124 void util_free(void *p) { mem_free(p); }
125 void *util_realloc(void *oldp,size_t n,size_t size) { return mem_realloc(oldp, n * size); }
126 
127 public import dmd.backend.cgcs : comsubs, cgcs_term;
128 public import dmd.backend.evalu8;
129 
130 void err_nomem() @nogc nothrow @trusted
131 {
132     printf("Error: out of memory\n");
133     err_exit();
134 }
135 
136 void symbol_keep(Symbol *s) { }
137 public import dmd.backend.symbol : symbol_print, symbol_term, symbol_ident, symbol_calloc,
138     symbol_name, symbol_generate, symbol_genauto, symbol_genauto, symbol_genauto,
139     symbol_func, symbol_funcalias, meminit_free, baseclass_find, baseclass_find_nest,
140     baseclass_nitems, symbol_free, symbol_add, symbol_add, symbol_insert, freesymtab,
141     symbol_copy, symbol_reset, symbol_pointerType;
142 
143 public import dmd.backend.cg87 : loadconst, cg87_reset;
144 
145 public import dmd.backend.cod3 : cod3_thunk;
146 
147 public import dmd.backend.dout : outthunk, out_readonly, out_readonly_comdat,
148     out_regcand, writefunc, alignOffset, out_reset, out_readonly_sym, out_string_literal, outdata;
149 
150 public import dmd.backend.blockopt : bc_goal, block_calloc, block_init, block_term, block_next,
151     block_next, block_goto, block_goto, block_goto, block_goto, block_ptr, block_pred,
152     block_clearvisit, block_visit, block_compbcount, blocklist_free, block_optimizer_free,
153     block_free, block_appendexp, block_endfunc, brcombine, blockopt, compdfo;
154 
155 public import dmd.backend.var : regstring;
156 public import dmd.backend.debugprint;
157 public import dmd.backend.cgelem : doptelem, postoptelem, elemisone;
158 public import dmd.backend.gloop : dom;
159 public import dmd.backend.util2 : binary;
160 
161 public import dmd.backend.go : go_flag, optfunc;
162 public import dmd.backend.drtlsym : rtlsym_init, rtlsym_reset, rtlsym_term;
163 public import dmd.backend.compress : id_compress;
164 public import dmd.backend.dwarfdbginf : dwarf_CFA_set_loc, dwarf_CFA_set_reg_offset,
165     dwarf_CFA_offset, dwarf_CFA_args_size;