|
22 | 22 | #include <util/pointer_offset_size.h> |
23 | 23 | #include <util/i2string.h> |
24 | 24 | #include <util/prefix.h> |
| 25 | +#include <ansi-c/c_types.h> |
| 26 | +#include <ansi-c/string_constant.h> |
25 | 27 |
|
26 | 28 | #include <goto-programs/goto_functions.h> |
27 | 29 |
|
@@ -132,6 +134,128 @@ bool java_static_lifetime_init( |
132 | 134 | return false; |
133 | 135 | } |
134 | 136 |
|
| 137 | + |
| 138 | +/*******************************************************************\ |
| 139 | +
|
| 140 | +Function: java_build_arguments |
| 141 | +
|
| 142 | + Inputs: |
| 143 | +
|
| 144 | + Outputs: |
| 145 | +
|
| 146 | + Purpose: |
| 147 | +
|
| 148 | +\*******************************************************************/ |
| 149 | + |
| 150 | +exprt::operandst java_build_arguments( |
| 151 | + const symbolt &function, |
| 152 | + code_blockt &init_code, |
| 153 | + symbol_tablet &symbol_table) |
| 154 | +{ |
| 155 | + const code_typet::parameterst ¶meters= |
| 156 | + to_code_type(function.type).parameters(); |
| 157 | + |
| 158 | + exprt::operandst main_arguments; |
| 159 | + main_arguments.resize(parameters.size()); |
| 160 | + |
| 161 | + for(std::size_t param_number=0; |
| 162 | + param_number<parameters.size(); |
| 163 | + param_number++) |
| 164 | + { |
| 165 | + bool is_this=param_number==0 && |
| 166 | + parameters[param_number].get_this(); |
| 167 | + bool allow_null=config.main!="" && !is_this; |
| 168 | + |
| 169 | + main_arguments[param_number]= |
| 170 | + object_factory(parameters[param_number].type(), |
| 171 | + init_code, allow_null, symbol_table); |
| 172 | + |
| 173 | + const symbolt &p_symbol= |
| 174 | + symbol_table.lookup(parameters[param_number].get_identifier()); |
| 175 | + |
| 176 | + // record as an input |
| 177 | + codet input(ID_input); |
| 178 | + input.operands().resize(2); |
| 179 | + input.op0()=address_of_exprt( |
| 180 | + index_exprt(string_constantt(p_symbol.base_name), |
| 181 | + gen_zero(index_type()))); |
| 182 | + input.op1()=main_arguments[param_number]; |
| 183 | + input.add_source_location()=parameters[param_number].source_location(); |
| 184 | + |
| 185 | + init_code.move_to_operands(input); |
| 186 | + } |
| 187 | + |
| 188 | + return main_arguments; |
| 189 | +} |
| 190 | + |
| 191 | +/*******************************************************************\ |
| 192 | +
|
| 193 | +Function: java_record_outputs |
| 194 | +
|
| 195 | + Inputs: |
| 196 | +
|
| 197 | + Outputs: |
| 198 | +
|
| 199 | + Purpose: |
| 200 | +
|
| 201 | +\*******************************************************************/ |
| 202 | + |
| 203 | +void java_record_outputs( |
| 204 | + const symbolt &function, |
| 205 | + const exprt::operandst &main_arguments, |
| 206 | + code_blockt &init_code, |
| 207 | + symbol_tablet &symbol_table) |
| 208 | +{ |
| 209 | + const code_typet::parameterst ¶meters= |
| 210 | + to_code_type(function.type).parameters(); |
| 211 | + |
| 212 | + exprt::operandst result; |
| 213 | + result.reserve(parameters.size()+1); |
| 214 | + |
| 215 | + bool has_return_value= |
| 216 | + to_code_type(function.type).return_type()!=empty_typet(); |
| 217 | + |
| 218 | + if(has_return_value) |
| 219 | + { |
| 220 | + //record return value |
| 221 | + codet output(ID_output); |
| 222 | + output.operands().resize(2); |
| 223 | + |
| 224 | + const symbolt &return_symbol=symbol_table.lookup("return'"); |
| 225 | + |
| 226 | + output.op0()=address_of_exprt( |
| 227 | + index_exprt(string_constantt(return_symbol.base_name), |
| 228 | + gen_zero(index_type()))); |
| 229 | + output.op1()=return_symbol.symbol_expr(); |
| 230 | + output.add_source_location()= |
| 231 | + function.value.operands().back().source_location(); |
| 232 | + |
| 233 | + init_code.move_to_operands(output); |
| 234 | + } |
| 235 | + |
| 236 | + for(std::size_t param_number=0; |
| 237 | + param_number<parameters.size(); |
| 238 | + param_number++) |
| 239 | + { |
| 240 | + const symbolt &p_symbol= |
| 241 | + symbol_table.lookup(parameters[param_number].get_identifier()); |
| 242 | + |
| 243 | + if(p_symbol.type.id()==ID_pointer) |
| 244 | + { |
| 245 | + // record as an output |
| 246 | + codet output(ID_output); |
| 247 | + output.operands().resize(2); |
| 248 | + output.op0()=address_of_exprt( |
| 249 | + index_exprt(string_constantt(p_symbol.base_name), |
| 250 | + gen_zero(index_type()))); |
| 251 | + output.op1()=main_arguments[param_number]; |
| 252 | + output.add_source_location()=parameters[param_number].source_location(); |
| 253 | + |
| 254 | + init_code.move_to_operands(output); |
| 255 | + } |
| 256 | + } |
| 257 | +} |
| 258 | + |
135 | 259 | /*******************************************************************\ |
136 | 260 |
|
137 | 261 | Function: java_entry_point |
@@ -287,8 +411,6 @@ bool java_entry_point( |
287 | 411 | assert(!symbol.value.is_nil()); |
288 | 412 | assert(symbol.type.id()==ID_code); |
289 | 413 |
|
290 | | - const code_typet &code_type=to_code_type(symbol.type); |
291 | | - |
292 | 414 | create_initialize(symbol_table); |
293 | 415 |
|
294 | 416 | if(java_static_lifetime_init(symbol_table, symbol.location, message_handler)) |
@@ -321,30 +443,27 @@ bool java_entry_point( |
321 | 443 | code_function_callt call_main; |
322 | 444 | call_main.add_source_location()=symbol.location; |
323 | 445 | call_main.function()=symbol.symbol_expr(); |
324 | | - |
325 | | - const code_typet::parameterst ¶meters= |
326 | | - code_type.parameters(); |
327 | | - |
328 | | - exprt::operandst main_arguments; |
329 | | - main_arguments.resize(parameters.size()); |
330 | | - |
331 | | - for(std::size_t param_number=0; |
332 | | - param_number<parameters.size(); |
333 | | - param_number++) |
| 446 | + if(to_code_type(symbol.type).return_type()!=empty_typet()) |
334 | 447 | { |
335 | | - bool is_this=param_number==0 && |
336 | | - parameters[param_number].get_this(); |
337 | | - bool allow_null=config.main!="" && !is_this; |
338 | | - |
339 | | - main_arguments[param_number]= |
340 | | - object_factory(parameters[param_number].type(), |
341 | | - init_code, allow_null, symbol_table); |
| 448 | + auxiliary_symbolt return_symbol; |
| 449 | + return_symbol.mode=ID_C; |
| 450 | + return_symbol.is_static_lifetime=false; |
| 451 | + return_symbol.name="return'"; |
| 452 | + return_symbol.base_name="return"; |
| 453 | + return_symbol.type=to_code_type(symbol.type).return_type(); |
| 454 | + |
| 455 | + symbol_table.add(return_symbol); |
| 456 | + call_main.lhs()=return_symbol.symbol_expr(); |
342 | 457 | } |
343 | 458 |
|
| 459 | + exprt::operandst main_arguments= |
| 460 | + java_build_arguments(symbol, init_code, symbol_table); |
344 | 461 | call_main.arguments()=main_arguments; |
345 | 462 |
|
346 | 463 | init_code.move_to_operands(call_main); |
347 | 464 |
|
| 465 | + java_record_outputs(symbol, main_arguments, init_code, symbol_table); |
| 466 | + |
348 | 467 | // add "main" |
349 | 468 | symbolt new_symbol; |
350 | 469 |
|
|
0 commit comments