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
16 * Szabo, Janos Zoltan – initial implementation
18 ******************************************************************************/
20 #include "../common/memory.h"
22 #include "CodeGenHelper.hh"
28 // =================================
30 // =================================
32 void Code::init_output(output_struct
*output
)
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
;
54 output
->functions
.get_param
= NULL
;
55 output
->functions
.log_param
= NULL
;
56 output
->functions
.init_comp
= NULL
;
57 output
->functions
.start
= NULL
;
58 output
->functions
.control
= NULL
;
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));
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;
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
;
86 void Code::merge_output(output_struct
*dest
, output_struct
*src
)
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
);
133 dest
->functions
.get_param
=
134 mputstr(dest
->functions
.get_param
, src
->functions
.get_param
);
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
);
145 void Code::free_output(output_struct
*output
)
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
);
167 Free(output
->functions
.get_param
);
168 Free(output
->functions
.log_param
);
169 Free(output
->functions
.init_comp
);
170 Free(output
->functions
.start
);
171 Free(output
->functions
.control
);
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
);
179 void Code::init_cdef(const_def
*cdef
)
187 void Code::merge_cdef(output_struct
*dest
, const_def
*cdef
)
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
);
194 void Code::free_cdef(const_def
*cdef
)
202 void Code::init_expr(expression_struct
*expr
)
204 expr
->preamble
= NULL
;
206 expr
->postamble
= NULL
;
209 void Code::clean_expr(expression_struct
*expr
)
215 void Code::free_expr(expression_struct
*expr
)
217 Free(expr
->preamble
);
219 Free(expr
->postamble
);
222 char* Code::merge_free_expr(char* str
, expression_struct
*expr
,
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
);
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");
246 char *Code::translate_character(char *str
, char c
, bool in_string
)
248 int i
= (unsigned char)c
;
251 return mputstr(str
, "\\a");
253 return mputstr(str
, "\\b");
255 return mputstr(str
, "\\f");
257 return mputstr(str
, "\\n");
259 return mputstr(str
, "\\r");
261 return mputstr(str
, "\\t");
263 return mputstr(str
, "\\v");
265 return mputstr(str
, "\\\\");
267 if (in_string
) return mputc(str
, '\'');
268 else return mputstr(str
, "\\'");
270 if (in_string
) return mputstr(str
, "\\\"");
271 else return mputc(str
, '"');
273 // to avoid recognition of trigraphs
274 if (in_string
) return mputstr(str
, "\\?");
275 else return mputc(str
, '?');
277 if (isascii(i
) && isprint(i
)) return mputc(str
, c
);
278 return mputprintf(str
, in_string
? "\\%03o" : "\\%o", i
);
282 char *Code::translate_string(char *str
, const char *src
)
284 for (size_t i
= 0; src
[i
] != '\0'; i
++)
285 str
= translate_character(str
, src
[i
], true);
289 } // namespace Common