implemented new code splitting mechanism (split to equal slices)
[deliverable/titan.core.git] / compiler2 / Code.cc
CommitLineData
d44e3c4f 1/******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Baranyi, Botond
11 * Cserveni, Akos
12 * Forstner, Matyas
13 * Kovacs, Ferenc
14 * Raduly, Csaba
14e21cff 15 * Szabo, Bence Janos
d44e3c4f 16 * Szabo, Janos Zoltan – initial implementation
17 *
18 ******************************************************************************/
970ed795
EL
19#include "Code.hh"
20#include "../common/memory.h"
21#include "error.h"
14e21cff 22#include "CodeGenHelper.hh"
970ed795
EL
23
24#include <ctype.h>
25
26namespace Common {
27
28 // =================================
29 // ===== Code
30 // =================================
31
32 void Code::init_output(output_struct *output)
33 {
34 output->header.includes = NULL;
35 output->header.class_decls = NULL;
36 output->header.typedefs = NULL;
37 output->header.class_defs = NULL;
38 output->header.function_prototypes = NULL;
39 output->header.global_vars = NULL;
40 output->header.testport_includes = NULL;
41 output->source.includes = NULL;
42 output->source.static_function_prototypes = NULL;
43 output->source.static_conversion_function_prototypes = NULL;
44 output->source.string_literals = NULL;
45 output->source.class_defs = NULL;
46 output->source.global_vars = NULL;
47 output->source.methods = NULL;
48 output->source.function_bodies = NULL;
49 output->source.static_function_bodies = NULL;
50 output->source.static_conversion_function_bodies = NULL;
51 output->functions.pre_init = NULL;
52 output->functions.post_init = NULL;
53 output->functions.set_param = NULL;
3abe9331 54 output->functions.get_param = NULL;
970ed795
EL
55 output->functions.log_param = NULL;
56 output->functions.init_comp = NULL;
57 output->functions.start = NULL;
58 output->functions.control = NULL;
14e21cff 59 output->intervals.pre_things_size = 0;
60 output->intervals.methods_size = 0;
61 output->intervals.function_bodies_size = 0;
62 output->intervals.static_conversion_function_bodies_size = 0;
63 output->intervals.static_function_bodies_size = 0;
64 output->intervals.methods_max_size = 1;
65 output->intervals.function_bodies_max_size = 1;
66 output->intervals.static_conversion_function_bodies_max_size = 1;
67 output->intervals.static_function_bodies_max_size = 1;
68 if (CodeGenHelper::GetInstance().get_split_mode() == CodeGenHelper::SPLIT_TO_SLICES) {
69 output->intervals.methods = (size_t*)Malloc(output->intervals.methods_max_size * sizeof(size_t));
70 output->intervals.function_bodies = (size_t*)Malloc(output->intervals.function_bodies_max_size * sizeof(size_t));
71 output->intervals.static_conversion_function_bodies = (size_t*)Malloc(output->intervals.static_conversion_function_bodies_max_size * sizeof(size_t));
72 output->intervals.static_function_bodies = (size_t*)Malloc(output->intervals.static_function_bodies_max_size * sizeof(size_t));
73
74 output->intervals.methods[0] = 0;
75 output->intervals.function_bodies[0] = 0;
76 output->intervals.static_conversion_function_bodies[0] = 0;
77 output->intervals.static_function_bodies[0] = 0;
78 } else {
79 output->intervals.methods = NULL;
80 output->intervals.function_bodies = NULL;
81 output->intervals.static_conversion_function_bodies = NULL;
82 output->intervals.static_function_bodies = NULL;
83 }
970ed795
EL
84 }
85
86 void Code::merge_output(output_struct *dest, output_struct *src)
87 {
88 dest->header.includes =
89 mputstr(dest->header.includes, src->header.includes);
90 dest->header.class_decls =
91 mputstr(dest->header.class_decls, src->header.class_decls);
92 dest->header.typedefs =
93 mputstr(dest->header.typedefs, src->header.typedefs);
94 dest->header.class_defs =
95 mputstr(dest->header.class_defs, src->header.class_defs);
96 dest->header.function_prototypes =
97 mputstr(dest->header.function_prototypes,
98 src->header.function_prototypes);
99 dest->header.global_vars =
100 mputstr(dest->header.global_vars, src->header.global_vars);
101 dest->header.testport_includes =
102 mputstr(dest->header.testport_includes, src->header.testport_includes);
103 dest->source.includes =
104 mputstr(dest->source.includes, src->source.includes);
105 dest->source.static_function_prototypes =
106 mputstr(dest->source.static_function_prototypes,
107 src->source.static_function_prototypes);
108 dest->source.static_conversion_function_prototypes =
109 mputstr(dest->source.static_conversion_function_prototypes,
110 src->source.static_conversion_function_prototypes);
111 dest->source.string_literals =
112 mputstr(dest->source.string_literals, src->source.string_literals);
113 dest->source.class_defs =
114 mputstr(dest->source.class_defs, src->source.class_defs);
115 dest->source.global_vars =
116 mputstr(dest->source.global_vars, src->source.global_vars);
117 dest->source.methods =
118 mputstr(dest->source.methods, src->source.methods);
119 dest->source.function_bodies =
120 mputstr(dest->source.function_bodies, src->source.function_bodies);
121 dest->source.static_function_bodies =
122 mputstr(dest->source.static_function_bodies,
123 src->source.static_function_bodies);
124 dest->source.static_conversion_function_bodies =
125 mputstr(dest->source.static_conversion_function_bodies,
126 src->source.static_conversion_function_bodies);
127 dest->functions.pre_init =
128 mputstr(dest->functions.pre_init, src->functions.pre_init);
129 dest->functions.post_init =
130 mputstr(dest->functions.post_init, src->functions.post_init);
131 dest->functions.set_param =
132 mputstr(dest->functions.set_param, src->functions.set_param);
3abe9331 133 dest->functions.get_param =
134 mputstr(dest->functions.get_param, src->functions.get_param);
970ed795
EL
135 dest->functions.log_param =
136 mputstr(dest->functions.log_param, src->functions.log_param);
137 dest->functions.init_comp =
138 mputstr(dest->functions.init_comp, src->functions.init_comp);
139 dest->functions.start =
140 mputstr(dest->functions.start, src->functions.start);
141 dest->functions.control =
142 mputstr(dest->functions.control, src->functions.control);
143 }
144
145 void Code::free_output(output_struct *output)
146 {
147 Free(output->header.includes);
148 Free(output->header.class_decls);
149 Free(output->header.typedefs);
150 Free(output->header.class_defs);
151 Free(output->header.function_prototypes);
152 Free(output->header.global_vars);
153 Free(output->header.testport_includes);
154 Free(output->source.includes);
155 Free(output->source.static_function_prototypes);
156 Free(output->source.static_conversion_function_prototypes);
157 Free(output->source.string_literals);
158 Free(output->source.class_defs);
159 Free(output->source.global_vars);
160 Free(output->source.methods);
161 Free(output->source.function_bodies);
162 Free(output->source.static_function_bodies);
163 Free(output->source.static_conversion_function_bodies);
164 Free(output->functions.pre_init);
165 Free(output->functions.post_init);
166 Free(output->functions.set_param);
3abe9331 167 Free(output->functions.get_param);
970ed795
EL
168 Free(output->functions.log_param);
169 Free(output->functions.init_comp);
170 Free(output->functions.start);
171 Free(output->functions.control);
14e21cff 172 Free(output->intervals.methods);
173 Free(output->intervals.function_bodies);
174 Free(output->intervals.static_conversion_function_bodies);
175 Free(output->intervals.static_function_bodies);
970ed795
EL
176 init_output(output);
177 }
178
179 void Code::init_cdef(const_def *cdef)
180 {
181 cdef->decl = NULL;
182 cdef->def = NULL;
183 //cdef->cdef = NULL;
184 cdef->init = NULL;
185 }
186
187 void Code::merge_cdef(output_struct *dest, const_def *cdef)
188 {
189 dest->header.global_vars = mputstr(dest->header.global_vars, cdef->decl);
190 dest->source.global_vars = mputstr(dest->source.global_vars, cdef->def);
191 dest->functions.pre_init = mputstr(dest->functions.pre_init, cdef->init);
192 }
193
194 void Code::free_cdef(const_def *cdef)
195 {
196 Free(cdef->decl);
197 Free(cdef->def);
198 //Free(cdef->cdef);
199 Free(cdef->init);
200 }
201
202 void Code::init_expr(expression_struct *expr)
203 {
204 expr->preamble = NULL;
205 expr->expr = NULL;
206 expr->postamble = NULL;
207 }
208
209 void Code::clean_expr(expression_struct *expr)
210 {
211 Free(expr->expr);
212 expr->expr = NULL;
213 }
214
215 void Code::free_expr(expression_struct *expr)
216 {
217 Free(expr->preamble);
218 Free(expr->expr);
219 Free(expr->postamble);
220 }
221
222 char* Code::merge_free_expr(char* str, expression_struct *expr,
223 bool is_block)
224 {
225 if (expr->preamble || expr->postamble) {
226 // open a statement block if the expression has a preamble or postamble
227 str = mputstr(str, "{\n");
228 // append the preamble if present
229 if (expr->preamble) str = mputstr(str, expr->preamble);
230 }
231 // append the expression itself
232 str = mputstr(str, expr->expr);
233 // terminate it with a bracket or semi-colon
234 if (is_block) str = mputstr(str, "}\n");
235 else str = mputstr(str, ";\n");
236 if (expr->preamble || expr->postamble) {
237 // append the postamble if present
238 if (expr->postamble) str = mputstr(str, expr->postamble);
239 // close the statement block
240 str = mputstr(str, "}\n");
241 }
242 free_expr(expr);
243 return str;
244 }
245
246 char *Code::translate_character(char *str, char c, bool in_string)
247 {
248 int i = (unsigned char)c;
249 switch (i) {
250 case '\a':
251 return mputstr(str, "\\a");
252 case '\b':
253 return mputstr(str, "\\b");
254 case '\f':
255 return mputstr(str, "\\f");
256 case '\n':
257 return mputstr(str, "\\n");
258 case '\r':
259 return mputstr(str, "\\r");
260 case '\t':
261 return mputstr(str, "\\t");
262 case '\v':
263 return mputstr(str, "\\v");
264 case '\\':
265 return mputstr(str, "\\\\");
266 case '\'':
267 if (in_string) return mputc(str, '\'');
268 else return mputstr(str, "\\'");
269 case '"':
270 if (in_string) return mputstr(str, "\\\"");
271 else return mputc(str, '"');
272 case '?':
273 // to avoid recognition of trigraphs
274 if (in_string) return mputstr(str, "\\?");
275 else return mputc(str, '?');
276 default:
277 if (isascii(i) && isprint(i)) return mputc(str, c);
278 return mputprintf(str, in_string ? "\\%03o" : "\\%o", i);
279 }
280 }
281
282 char *Code::translate_string(char *str, const char *src)
283 {
284 for (size_t i = 0; src[i] != '\0'; i++)
285 str = translate_character(str, src[i], true);
286 return str;
287 }
288
289} // namespace Common
This page took 0.044688 seconds and 5 git commands to generate.