1 /**
2  * Compiler implementation of the
3  * $(LINK2 http://www.dlang.org, D programming language).
4  *
5  * Copyright:   Copyright (C) 1987-1998 by Symantec
6  *              Copyright (C) 2000-2020 by The D Language Foundation, All Rights Reserved
7  * Authors:     $(LINK2 http://www.digitalmars.com, Walter Bright)
8  * License:     $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
9  * Source:      $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/backend/dcode.d, backend/dcode.d)
10  */
11 
12 module dmd.backend.dcode;
13 
14 version (SCPP)
15     version = COMPILE;
16 version (MARS)
17     version = COMPILE;
18 
19 version (COMPILE)
20 {
21 
22 import core.stdc.stdio;
23 import core.stdc.stdlib;
24 import core.stdc.string;
25 
26 import dmd.backend.cc;
27 import dmd.backend.cdef;
28 import dmd.backend.code;
29 import dmd.backend.code_x86;
30 import dmd.backend.global;
31 import dmd.backend.mem;
32 
33 extern (C++):
34 
35 nothrow:
36 
37 __gshared
38 code *code_list = null;
39 
40 /************************************
41  * Allocate a chunk of code's and add them to
42  * code_list.
43  */
44 code *code_chunk_alloc()
45 {
46     const size_t n = 4096 / code.sizeof;
47     //printf("code_chunk_alloc() n = %d\n", n);
48     code *chunk = cast(code *)mem_fmalloc(n * code.sizeof);
49     for (size_t i = 0; i < n - 1; ++i)
50     {
51         chunk[i].next = &chunk[i + 1];
52     }
53     chunk[n - 1].next = null;
54     code_list = chunk;
55     return chunk;
56 }
57 
58 /*****************
59  * Allocate code
60  */
61 
62 code *code_calloc()
63 {
64     //printf("code %d\n", code.sizeof);
65     code *c = code_list ? code_list : code_chunk_alloc();
66     code_list = code_next(c);
67     memset(c, 0, code.sizeof);
68 
69     //dbg_printf("code_calloc: %p\n",c);
70     return c;
71 }
72 
73 
74 /*****************
75  * Free code
76  */
77 
78 void code_free(code *cstart)
79 {
80     if (cstart)
81     {
82         code *c = cstart;
83         while (1)
84         {
85             if (c.Iop == ASM)
86             {
87                 mem_free(c.IEV1.bytes);
88             }
89             code *cnext = code_next(c);
90             if (!cnext)
91                 break;
92             c = cnext;
93         }
94         c.next = code_list;
95         code_list = cstart;
96     }
97 }
98 
99 /*****************
100  * Terminate code
101  */
102 
103 void code_term()
104 {
105 static if (TERMCODE)
106 {
107     code *cn;
108     int count = 0;
109 
110     while (code_list)
111     {   cn = code_next(code_list);
112         //mem_ffree(code_list);
113         code_list = cn;
114         count++;
115     }
116     debug printf("Max # of codes = %d\n",count);
117 }
118 else
119 {
120 debug
121 {
122     int count = 0;
123 
124     for (code *cn = code_list; cn; cn = code_next(cn))
125         count++;
126     printf("Max # of codes = %d\n",count);
127 }
128 }
129 }
130 
131 }