| 1 |
/* |
| 2 |
* Copyright (c) 2021-2025 Symas Corporation |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are |
| 6 |
* met: |
| 7 |
* |
| 8 |
* * Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* * Redistributions in binary form must reproduce the above |
| 11 |
* copyright notice, this list of conditions and the following disclaimer |
| 12 |
* in the documentation and/or other materials provided with the |
| 13 |
* distribution. |
| 14 |
* * Neither the name of the Symas Corporation nor the names of its |
| 15 |
* contributors may be used to endorse or promote products derived from |
| 16 |
* this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
#include "cobol-system.h" |
| 31 |
#include "coretypes.h" |
| 32 |
#include "tree.h" |
| 33 |
#include "../../libgcobol/ec.h" |
| 34 |
#include "../../libgcobol/common-defs.h" |
| 35 |
#include "util.h" |
| 36 |
#include "cbldiag.h" |
| 37 |
#include "symbols.h" |
| 38 |
#include "inspect.h" |
| 39 |
#include "../../libgcobol/io.h" |
| 40 |
#include "genapi.h" |
| 41 |
#include "genutil.h" |
| 42 |
#include "gengen.h" |
| 43 |
#include "structs.h" |
| 44 |
#include "../../libgcobol/gcobolio.h" |
| 45 |
#include "show_parse.h" |
| 46 |
|
| 47 |
void |
| 48 |
set_up_on_exception_label(cbl_label_t *arithmetic_label) |
| 49 |
{ |
| 50 |
if( arithmetic_label ) |
| 51 |
{ |
| 52 |
if( !arithmetic_label->structs.arith_error ) |
| 53 |
{ |
| 54 |
arithmetic_label->structs.arith_error |
| 55 |
= static_cast<cbl_arith_error_t *> |
| 56 |
(xmalloc(sizeof(struct cbl_arith_error_t))); |
| 57 |
// Set up the address pairs for this clause |
| 58 |
gg_create_goto_pair(&arithmetic_label->structs.arith_error->over.go_to, |
| 59 |
&arithmetic_label->structs.arith_error->over.label); |
| 60 |
gg_create_goto_pair(&arithmetic_label->structs.arith_error->into.go_to, |
| 61 |
&arithmetic_label->structs.arith_error->into.label); |
| 62 |
gg_create_goto_pair(&arithmetic_label->structs.arith_error->bottom.go_to, |
| 63 |
&arithmetic_label->structs.arith_error->bottom.label); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
void |
| 69 |
set_up_compute_error_label(cbl_label_t *compute_label) |
| 70 |
{ |
| 71 |
if( compute_label ) |
| 72 |
{ |
| 73 |
if( !compute_label->structs.compute_error ) |
| 74 |
{ |
| 75 |
compute_label->structs.compute_error |
| 76 |
= static_cast<cbl_compute_error_t *> |
| 77 |
(xmalloc(sizeof(struct cbl_compute_error_t))); |
| 78 |
compute_label->structs.compute_error->compute_error_code |
| 79 |
= gg_define_int(0); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
static void |
| 85 |
set_up_arithmetic_error_handler(cbl_label_t *error, |
| 86 |
cbl_label_t *not_error) |
| 87 |
{ |
| 88 |
Analyze(); |
| 89 |
// There might, or might not, be error and/or not_error labels: |
| 90 |
set_up_on_exception_label(error); |
| 91 |
set_up_on_exception_label(not_error); |
| 92 |
} |
| 93 |
|
| 94 |
static void |
| 95 |
arithmetic_operation(size_t nC, cbl_num_result_t *C, |
| 96 |
size_t nA, cbl_refer_t *A, |
| 97 |
size_t nB, cbl_refer_t *B, |
| 98 |
cbl_arith_format_t format, |
| 99 |
const cbl_label_t *error, |
| 100 |
const cbl_label_t *not_error, |
| 101 |
tree compute_error, // Pointer to int |
| 102 |
const char *operation, |
| 103 |
cbl_refer_t *remainder = NULL) |
| 104 |
{ |
| 105 |
Analyze(); |
| 106 |
SHOW_PARSE |
| 107 |
{ |
| 108 |
SHOW_PARSE_HEADER |
| 109 |
SHOW_PARSE_TEXT_AB("performing ", operation, "") |
| 110 |
} |
| 111 |
|
| 112 |
TRACE1 |
| 113 |
{ |
| 114 |
TRACE1_HEADER |
| 115 |
TRACE1_TEXT_ABC("calling ", operation, "") |
| 116 |
for(size_t ii=0; ii<nA; ii++) |
| 117 |
{ |
| 118 |
TRACE1_INDENT |
| 119 |
gg_fprintf( trace_handle, |
| 120 |
1, "parameter A[%ld]: ", |
| 121 |
build_int_cst_type(SIZE_T, ii)); |
| 122 |
TRACE1_REFER("", A[ii], ""); |
| 123 |
} |
| 124 |
for(size_t ii=0; ii<nB; ii++) |
| 125 |
{ |
| 126 |
TRACE1_INDENT |
| 127 |
gg_fprintf( trace_handle, |
| 128 |
1, "parameter B[%ld]: ", |
| 129 |
build_int_cst_type(SIZE_T, ii)); |
| 130 |
TRACE1_REFER("", B[ii], ""); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
// We need to split up cbl_num_result_t into two arrays, one for the refer_t |
| 135 |
// and a second for the cbl_round_t enums. |
| 136 |
|
| 137 |
// Allocate nC+1 in case this is a divide with a REMAINDER |
| 138 |
|
| 139 |
std::vector<cbl_refer_t> results(nC + 1); |
| 140 |
int ncount = 0; |
| 141 |
|
| 142 |
if( nC+1 <= MIN_FIELD_BLOCK_SIZE ) |
| 143 |
{ |
| 144 |
// We know there is room in our existing buffer |
| 145 |
} |
| 146 |
else |
| 147 |
{ |
| 148 |
// We might have to allocate more space: |
| 149 |
gg_call(VOID, |
| 150 |
"__gg__resize_int_p", |
| 151 |
gg_get_address_of(var_decl_arithmetic_rounds_size), |
| 152 |
gg_get_address_of(var_decl_arithmetic_rounds), |
| 153 |
build_int_cst_type(SIZE_T, nC+1), |
| 154 |
NULL_TREE); |
| 155 |
} |
| 156 |
|
| 157 |
// We have to take into account the possibility the quotient of the division |
| 158 |
// can affect the disposition of the remainder. In particular, some of the |
| 159 |
// NIST tests have the construction |
| 160 |
|
| 161 |
// DIVIDE A BY B GIVING C REMAINDER TABLE(C) |
| 162 |
|
| 163 |
// Which seems, somehow, unnatural. |
| 164 |
|
| 165 |
cbl_refer_t temp_remainder; |
| 166 |
cbl_field_t temp_field = {}; |
| 167 |
|
| 168 |
if( remainder ) |
| 169 |
{ |
| 170 |
// We need a duplicate of the remainder, because we have to take into count |
| 171 |
// the possibility of a size error in moving the remainder into place |
| 172 |
temp_field.type = remainder->field->type; |
| 173 |
temp_field.attr = (remainder->field->attr | intermediate_e) & ~initialized_e; |
| 174 |
temp_field.level = 1; |
| 175 |
temp_field.data.memsize = remainder->field->data.memsize ; |
| 176 |
temp_field.data.capacity = remainder->field->data.capacity; |
| 177 |
temp_field.data.digits = remainder->field->data.digits ; |
| 178 |
temp_field.data.rdigits = remainder->field->data.rdigits ; |
| 179 |
temp_field.data.initial = remainder->field->data.initial ; |
| 180 |
temp_field.data.picture = remainder->field->data.picture ; |
| 181 |
parser_symbol_add(&temp_field); |
| 182 |
temp_remainder.field = &temp_field; |
| 183 |
|
| 184 |
// For division, the optional remainder goes onto the beginning of the |
| 185 |
// list |
| 186 |
results[ncount++] = temp_remainder; |
| 187 |
} |
| 188 |
for(size_t i=0; i<nC; i++) |
| 189 |
{ |
| 190 |
results[ncount] = C[i].refer; |
| 191 |
gg_assign( gg_array_value(var_decl_arithmetic_rounds, ncount), |
| 192 |
build_int_cst_type(INT, C[i].rounded)); |
| 193 |
ncount += 1; |
| 194 |
} |
| 195 |
|
| 196 |
// REMAINDER_PRESENT means what it says. |
| 197 |
// ON_SIZE_ERROR means that the ON SIZE ERROR phrase is present |
| 198 |
|
| 199 |
int call_flags = (( error || not_error ) ? ON_SIZE_ERROR : 0) |
| 200 |
+ (remainder ? REMAINDER_PRESENT : 0); |
| 201 |
|
| 202 |
gcc_assert(compute_error); |
| 203 |
|
| 204 |
// Having done all that work, we now need to break out the various different |
| 205 |
// arithmetic routines that implement the various possibilities, |
| 206 |
|
| 207 |
build_array_of_treeplets(1, nA, A); |
| 208 |
build_array_of_treeplets(2, nB, B); |
| 209 |
build_array_of_treeplets(3, ncount, results.data()); |
| 210 |
|
| 211 |
gg_call(VOID, |
| 212 |
operation, |
| 213 |
build_int_cst_type(INT, format), |
| 214 |
build_int_cst_type(SIZE_T, nA), |
| 215 |
build_int_cst_type(SIZE_T, nB), |
| 216 |
build_int_cst_type(SIZE_T, ncount), |
| 217 |
var_decl_arithmetic_rounds, |
| 218 |
build_int_cst_type(INT, call_flags), |
| 219 |
compute_error, |
| 220 |
NULL_TREE); |
| 221 |
TRACE1 |
| 222 |
{ |
| 223 |
for(size_t ii=0; ii<nC; ii++) |
| 224 |
{ |
| 225 |
TRACE1_INDENT |
| 226 |
gg_fprintf( trace_handle, |
| 227 |
1, "result: C[%ld]: ", |
| 228 |
build_int_cst_type(SIZE_T, ii)); |
| 229 |
TRACE1_REFER("", C[ii].refer, ""); |
| 230 |
} |
| 231 |
TRACE1_END |
| 232 |
} |
| 233 |
|
| 234 |
// We just did an operation. |
| 235 |
IF( gg_indirect(compute_error), ne_op, integer_zero_node ) |
| 236 |
{ |
| 237 |
gg_call( VOID, |
| 238 |
"__gg__process_compute_error", |
| 239 |
gg_indirect(compute_error), |
| 240 |
NULL_TREE); |
| 241 |
} |
| 242 |
ELSE |
| 243 |
ENDIF |
| 244 |
|
| 245 |
if( remainder ) |
| 246 |
{ |
| 247 |
parser_move(*remainder, temp_remainder); |
| 248 |
} |
| 249 |
|
| 250 |
SHOW_PARSE |
| 251 |
{ |
| 252 |
SHOW_PARSE_END |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
static void |
| 257 |
arithmetic_error_handler( cbl_label_t *error, |
| 258 |
cbl_label_t *not_error, |
| 259 |
tree compute_error) // Pointer to int with bits |
| 260 |
{ |
| 261 |
Analyze(); |
| 262 |
if( error ) |
| 263 |
{ |
| 264 |
// We had an ON SIZE ERROR phrase |
| 265 |
IF( gg_indirect(compute_error), ne_op, integer_zero_node) |
| 266 |
{ |
| 267 |
// The ON SIZE ERROR imperative takes precedence over exception processing |
| 268 |
// So, we set the global exception code to zero. This leaves intact the |
| 269 |
// stashed data needed for FUNCTION EXCEPTION-STATUS, but will preclude |
| 270 |
// any declarative processing |
| 271 |
gg_assign(var_decl_exception_code, integer_zero_node); |
| 272 |
|
| 273 |
// There was some kind of error, so we execute the ON SIZE ERROR |
| 274 |
// imperative |
| 275 |
gg_append_statement( error->structs.arith_error->into.go_to ); |
| 276 |
} |
| 277 |
ELSE |
| 278 |
ENDIF |
| 279 |
} |
| 280 |
|
| 281 |
if( not_error ) |
| 282 |
{ |
| 283 |
IF( gg_indirect(compute_error), eq_op, integer_zero_node) |
| 284 |
{ |
| 285 |
// There wasn't a computation error |
| 286 |
gg_append_statement( not_error->structs.arith_error->into.go_to ); |
| 287 |
} |
| 288 |
ELSE |
| 289 |
ENDIF |
| 290 |
} |
| 291 |
|
| 292 |
// With the operation and the two possible GO TOs laid down, it's time |
| 293 |
// to create the target labels for exiting the ON [NOT] SIZE ERROR blocks: |
| 294 |
if( error ) |
| 295 |
{ |
| 296 |
gg_append_statement( error->structs.arith_error->bottom.label ); |
| 297 |
} |
| 298 |
if( not_error ) |
| 299 |
{ |
| 300 |
gg_append_statement( not_error->structs.arith_error->bottom.label ); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
static bool |
| 305 |
is_somebody_float(size_t nA, const cbl_refer_t *A) |
| 306 |
{ |
| 307 |
bool retval = false; |
| 308 |
for(size_t i=0; i<nA; i++) |
| 309 |
{ |
| 310 |
if(A[i].field->type == FldFloat) |
| 311 |
{ |
| 312 |
retval = true; |
| 313 |
break; |
| 314 |
} |
| 315 |
} |
| 316 |
return retval; |
| 317 |
} |
| 318 |
|
| 319 |
static bool |
| 320 |
is_somebody_float(size_t nC, const cbl_num_result_t *C) |
| 321 |
{ |
| 322 |
bool retval = false; |
| 323 |
for(size_t i=0; i<nC; i++) |
| 324 |
{ |
| 325 |
if(C[i].refer.field->type == FldFloat) |
| 326 |
{ |
| 327 |
retval = true; |
| 328 |
break; |
| 329 |
} |
| 330 |
} |
| 331 |
return retval; |
| 332 |
} |
| 333 |
|
| 334 |
static bool |
| 335 |
all_results_binary(size_t nC, const cbl_num_result_t *C) |
| 336 |
{ |
| 337 |
bool retval = true; |
| 338 |
|
| 339 |
for(size_t i=0; i<nC; i++) |
| 340 |
{ |
| 341 |
if(C[i].refer.field->data.digits != 0 || C[i].refer.field->type == FldFloat ) |
| 342 |
{ |
| 343 |
retval = false; |
| 344 |
break; |
| 345 |
} |
| 346 |
} |
| 347 |
return retval; |
| 348 |
} |
| 349 |
|
| 350 |
static tree |
| 351 |
largest_binary_term(size_t nA, cbl_refer_t *A) |
| 352 |
{ |
| 353 |
tree retval = NULL_TREE; |
| 354 |
uint32_t max_capacity = 0; |
| 355 |
int is_negative = 0; |
| 356 |
|
| 357 |
for(size_t i=0; i<nA; i++) |
| 358 |
{ |
| 359 |
if( A[i].field->data.rdigits || A[i].field->type == FldFloat ) |
| 360 |
{ |
| 361 |
// We are prepared to work only with integers |
| 362 |
retval = NULL_TREE; |
| 363 |
break; |
| 364 |
} |
| 365 |
if( A[i].field->type == FldLiteralN |
| 366 |
// || A[i].field->type == FldNumericDisplay |
| 367 |
|| A[i].field->type == FldNumericBinary |
| 368 |
|| A[i].field->type == FldNumericBin5 |
| 369 |
|| A[i].field->type == FldIndex |
| 370 |
|| A[i].field->type == FldPointer ) |
| 371 |
{ |
| 372 |
// This is an integer type that can be worked with quickly |
| 373 |
is_negative |= ( A[i].field->attr & signable_e ); |
| 374 |
max_capacity = std::max(max_capacity, A[i].field->data.capacity); |
| 375 |
retval = tree_type_from_size(max_capacity, is_negative); |
| 376 |
} |
| 377 |
else |
| 378 |
{ |
| 379 |
// This is a type we don't care to deal with for fast arithmetic |
| 380 |
retval = NULL_TREE; |
| 381 |
break; |
| 382 |
} |
| 383 |
} |
| 384 |
return retval; |
| 385 |
} |
| 386 |
|
| 387 |
static bool |
| 388 |
fast_add( size_t nC, cbl_num_result_t *C, |
| 389 |
size_t nA, cbl_refer_t *A, |
| 390 |
cbl_arith_format_t format ) |
| 391 |
{ |
| 392 |
bool retval = false; |
| 393 |
if( all_results_binary(nC, C) ) |
| 394 |
{ |
| 395 |
Analyze(); |
| 396 |
// All targets are non-PICTURE binaries: |
| 397 |
//gg_insert_into_assembler("# DUBNER addition START"); |
| 398 |
tree term_type = largest_binary_term(nA, A); |
| 399 |
if( term_type ) |
| 400 |
{ |
| 401 |
// All the terms are things we can work with. |
| 402 |
|
| 403 |
// We need to calculate the sum of all the A[] terms using term_type as |
| 404 |
// the intermediate type: |
| 405 |
|
| 406 |
tree sum = gg_define_variable(term_type); |
| 407 |
tree addend = gg_define_variable(term_type); |
| 408 |
get_binary_value( sum, |
| 409 |
NULL, |
| 410 |
A[0].field, |
| 411 |
refer_offset(A[0])); |
| 412 |
|
| 413 |
// Add in the rest of them: |
| 414 |
for(size_t i=1; i<nA; i++) |
| 415 |
{ |
| 416 |
get_binary_value( addend, |
| 417 |
NULL, |
| 418 |
A[i].field, |
| 419 |
refer_offset(A[i])); |
| 420 |
gg_assign(sum, gg_add(sum, addend)); |
| 421 |
} |
| 422 |
//gg_printf("The intermediate sum is %ld\n", gg_cast(LONG, sum), NULL_TREE); |
| 423 |
|
| 424 |
// We now either accumulate into C[n] or assign to C[n]: |
| 425 |
for(size_t i=0; i<nC; i++ ) |
| 426 |
{ |
| 427 |
tree dest_type = tree_type_from_size(C[i].refer.field->data.capacity, 0); |
| 428 |
tree dest_addr = gg_add(member(C[i].refer.field->var_decl_node, "data"), |
| 429 |
refer_offset(C[i].refer)); |
| 430 |
tree ptr = gg_cast(build_pointer_type(dest_type), dest_addr); |
| 431 |
if( format == giving_e ) |
| 432 |
{ |
| 433 |
// We are assigning |
| 434 |
gg_assign( gg_indirect(ptr), |
| 435 |
gg_cast(dest_type, sum)); |
| 436 |
} |
| 437 |
else |
| 438 |
{ |
| 439 |
// We are accumulating |
| 440 |
gg_assign( gg_indirect(ptr), |
| 441 |
gg_add( gg_indirect(ptr), |
| 442 |
gg_cast(dest_type, sum))); |
| 443 |
} |
| 444 |
} |
| 445 |
retval = true; |
| 446 |
} |
| 447 |
|
| 448 |
//gg_insert_into_assembler("# DUBNER addition END "); |
| 449 |
} |
| 450 |
return retval; |
| 451 |
} |
| 452 |
|
| 453 |
static bool |
| 454 |
fast_subtract(size_t nC, cbl_num_result_t *C, |
| 455 |
size_t nA, cbl_refer_t *A, |
| 456 |
size_t nB, cbl_refer_t *B, |
| 457 |
cbl_arith_format_t format) |
| 458 |
{ |
| 459 |
bool retval = false; |
| 460 |
if( all_results_binary(nC, C) ) |
| 461 |
{ |
| 462 |
Analyze(); |
| 463 |
// All targets are non-PICTURE binaries: |
| 464 |
//gg_insert_into_assembler("# DUBNER addition START"); |
| 465 |
tree term_type = largest_binary_term(nA, A); |
| 466 |
|
| 467 |
if( term_type && format == giving_e ) |
| 468 |
{ |
| 469 |
tree term_type_B = largest_binary_term(nB, B); |
| 470 |
if( term_type_B ) |
| 471 |
{ |
| 472 |
if(TREE_INT_CST_LOW(TYPE_SIZE(term_type_B)) |
| 473 |
> TREE_INT_CST_LOW(TYPE_SIZE(term_type)) ) |
| 474 |
{ |
| 475 |
term_type = term_type_B; |
| 476 |
} |
| 477 |
} |
| 478 |
else |
| 479 |
{ |
| 480 |
term_type = NULL_TREE; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
if( term_type ) |
| 485 |
{ |
| 486 |
// All the terms are things we can work with. |
| 487 |
|
| 488 |
// We need to calculate the sum of all the A[] terms using term_type as |
| 489 |
// the intermediate type: |
| 490 |
|
| 491 |
tree sum = gg_define_variable(term_type); |
| 492 |
tree addend = gg_define_variable(term_type); |
| 493 |
get_binary_value(sum, NULL, A[0].field, refer_offset(A[0])); |
| 494 |
|
| 495 |
// Add in the rest of them: |
| 496 |
for(size_t i=1; i<nA; i++) |
| 497 |
{ |
| 498 |
get_binary_value(sum, NULL, A[i].field, refer_offset(A[i])); |
| 499 |
gg_assign(sum, gg_add(sum, addend)); |
| 500 |
} |
| 501 |
//gg_printf("The intermediate sum is %ld\n", gg_cast(LONG, sum), NULL_TREE); |
| 502 |
|
| 503 |
if( format == giving_e ) |
| 504 |
{ |
| 505 |
// We now subtract the sum from B[0] |
| 506 |
get_binary_value(addend, NULL, B[0].field, refer_offset(B[0])); |
| 507 |
gg_assign(sum, gg_subtract(addend, sum)); |
| 508 |
} |
| 509 |
|
| 510 |
// We now either accumulate into C[n] or assign to C[n]: |
| 511 |
for(size_t i=0; i<nC; i++ ) |
| 512 |
{ |
| 513 |
tree dest_type = tree_type_from_size(C[i].refer.field->data.capacity, 0); |
| 514 |
tree dest_addr = gg_add(member(C[i].refer.field->var_decl_node, "data"), |
| 515 |
refer_offset(C[i].refer)); |
| 516 |
tree ptr = gg_cast(build_pointer_type(dest_type), dest_addr); |
| 517 |
if( format == giving_e ) |
| 518 |
{ |
| 519 |
// We are assigning |
| 520 |
gg_assign( gg_indirect(ptr), |
| 521 |
gg_cast(dest_type, sum)); |
| 522 |
} |
| 523 |
else |
| 524 |
{ |
| 525 |
// We are subtracting the sum from C[i] |
| 526 |
gg_assign( gg_indirect(ptr), |
| 527 |
gg_subtract(gg_indirect(ptr), |
| 528 |
gg_cast(dest_type, sum))); |
| 529 |
} |
| 530 |
} |
| 531 |
retval = true; |
| 532 |
} |
| 533 |
} |
| 534 |
return retval; |
| 535 |
} |
| 536 |
|
| 537 |
static bool |
| 538 |
fast_multiply(size_t nC, cbl_num_result_t *C, |
| 539 |
size_t nA, cbl_refer_t *A, |
| 540 |
size_t nB, cbl_refer_t *B) |
| 541 |
{ |
| 542 |
bool retval = false; |
| 543 |
if( all_results_binary(nC, C) ) |
| 544 |
{ |
| 545 |
Analyze(); |
| 546 |
// All targets are non-PICTURE binaries: |
| 547 |
//gg_insert_into_assembler("# DUBNER addition START"); |
| 548 |
tree term_type = largest_binary_term(nA, A); |
| 549 |
|
| 550 |
if( term_type && nB ) |
| 551 |
{ |
| 552 |
tree term_type_B = largest_binary_term(nB, B); |
| 553 |
if( term_type_B ) |
| 554 |
{ |
| 555 |
if(TREE_INT_CST_LOW(TYPE_SIZE(term_type_B)) |
| 556 |
> TREE_INT_CST_LOW(TYPE_SIZE(term_type)) ) |
| 557 |
{ |
| 558 |
term_type = term_type_B; |
| 559 |
} |
| 560 |
} |
| 561 |
else |
| 562 |
{ |
| 563 |
term_type = NULL_TREE; |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
if( term_type ) |
| 568 |
{ |
| 569 |
// All the terms are things we can work with. |
| 570 |
|
| 571 |
tree valA = gg_define_variable(term_type); |
| 572 |
tree valB = gg_define_variable(term_type); |
| 573 |
get_binary_value(valA, NULL, A[0].field, refer_offset(A[0])); |
| 574 |
|
| 575 |
if( nB ) |
| 576 |
{ |
| 577 |
// This is a MULTIPLY Format 2 |
| 578 |
get_binary_value(valB, NULL, B[0].field, refer_offset(B[0])); |
| 579 |
gg_assign(valA, gg_multiply(valA, valB)); |
| 580 |
} |
| 581 |
|
| 582 |
// We now either multiply into C[n] or assign A * B to C[n]: |
| 583 |
for(size_t i=0; i<nC; i++ ) |
| 584 |
{ |
| 585 |
tree dest_type = tree_type_from_size(C[i].refer.field->data.capacity, 0); |
| 586 |
tree dest_addr = gg_add(member(C[i].refer.field->var_decl_node, "data"), |
| 587 |
refer_offset(C[i].refer)); |
| 588 |
tree ptr = gg_cast(build_pointer_type(dest_type), dest_addr); |
| 589 |
if( nB ) |
| 590 |
{ |
| 591 |
// We put A * B into C |
| 592 |
gg_assign(gg_indirect(ptr), gg_cast(dest_type, valA)); |
| 593 |
} |
| 594 |
else |
| 595 |
{ |
| 596 |
// We multiply C = valA * C |
| 597 |
gg_assign(gg_indirect(ptr), |
| 598 |
gg_multiply(gg_indirect(ptr), valA)); |
| 599 |
} |
| 600 |
} |
| 601 |
retval = true; |
| 602 |
} |
| 603 |
|
| 604 |
//gg_insert_into_assembler("# DUBNER addition END "); |
| 605 |
} |
| 606 |
return retval; |
| 607 |
} |
| 608 |
|
| 609 |
static bool |
| 610 |
fast_divide(size_t nC, cbl_num_result_t *C, |
| 611 |
size_t nA, cbl_refer_t *A, |
| 612 |
size_t nB, cbl_refer_t *B, |
| 613 |
const cbl_refer_t &remainder) |
| 614 |
{ |
| 615 |
bool retval = false; |
| 616 |
if( all_results_binary(nC, C) ) |
| 617 |
{ |
| 618 |
Analyze(); |
| 619 |
// All targets are non-PICTURE binaries: |
| 620 |
//gg_insert_into_assembler("# DUBNER addition START"); |
| 621 |
tree term_type = largest_binary_term(nA, A); |
| 622 |
|
| 623 |
if( term_type && nB ) |
| 624 |
{ |
| 625 |
tree term_type_B = largest_binary_term(nB, B); |
| 626 |
if( term_type_B ) |
| 627 |
{ |
| 628 |
if(TREE_INT_CST_LOW(TYPE_SIZE(term_type_B)) |
| 629 |
> TREE_INT_CST_LOW(TYPE_SIZE(term_type)) ) |
| 630 |
{ |
| 631 |
term_type = term_type_B; |
| 632 |
} |
| 633 |
} |
| 634 |
else |
| 635 |
{ |
| 636 |
term_type = NULL_TREE; |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
if( term_type ) |
| 641 |
{ |
| 642 |
// All the terms are things we can work with. |
| 643 |
|
| 644 |
tree divisor = gg_define_variable(term_type); |
| 645 |
tree dividend = gg_define_variable(term_type); |
| 646 |
tree quotient = NULL_TREE; |
| 647 |
get_binary_value(divisor, NULL, A[0].field, refer_offset(A[0])); |
| 648 |
|
| 649 |
if( nB ) |
| 650 |
{ |
| 651 |
// This is a MULTIPLY Format 2, where we are dividing A into B and |
| 652 |
// assigning that to C |
| 653 |
get_binary_value(dividend, NULL, B[0].field, refer_offset(B[0])); |
| 654 |
|
| 655 |
quotient = gg_define_variable(term_type); |
| 656 |
// Yes, in this case the divisor and dividend are switched. Things are |
| 657 |
// tough all over. |
| 658 |
gg_assign(quotient, gg_divide(divisor, dividend)); |
| 659 |
} |
| 660 |
|
| 661 |
// We now either divide into C[n] or assign dividend/divisor to C[n]: |
| 662 |
for(size_t i=0; i<nC; i++ ) |
| 663 |
{ |
| 664 |
tree dest_type = |
| 665 |
tree_type_from_size(C[i].refer.field->data.capacity, 0); |
| 666 |
tree dest_addr = gg_add(member( C[i].refer.field->var_decl_node, |
| 667 |
"data"), |
| 668 |
refer_offset(C[i].refer)); |
| 669 |
tree ptr = gg_cast(build_pointer_type(dest_type), dest_addr); |
| 670 |
if( nB ) |
| 671 |
{ |
| 672 |
// We put A * B into C |
| 673 |
gg_assign(gg_indirect(ptr), gg_cast(dest_type, quotient)); |
| 674 |
} |
| 675 |
else |
| 676 |
{ |
| 677 |
// We divide the divisor into C |
| 678 |
gg_assign(gg_indirect(ptr), |
| 679 |
gg_divide(gg_indirect(ptr), divisor)); |
| 680 |
} |
| 681 |
|
| 682 |
// This is where we handle any remainder, keeping in mind that for |
| 683 |
// nB != 0, the actual dividend is in the value we have named |
| 684 |
// "divisor". |
| 685 |
|
| 686 |
// We calculate the remainder by calculating |
| 687 |
// dividend minus quotient * divisor |
| 688 |
if( remainder.field ) |
| 689 |
{ |
| 690 |
dest_addr = gg_add( member(remainder.field->var_decl_node, "data"), |
| 691 |
refer_offset(remainder)); |
| 692 |
dest_type = tree_type_from_size(remainder.field->data.capacity, 0); |
| 693 |
ptr = gg_cast(build_pointer_type(dest_type), dest_addr); |
| 694 |
|
| 695 |
gg_assign(gg_indirect(ptr), |
| 696 |
gg_cast(dest_type, gg_subtract(divisor, |
| 697 |
gg_multiply(quotient, dividend)))); |
| 698 |
} |
| 699 |
} |
| 700 |
retval = true; |
| 701 |
} |
| 702 |
|
| 703 |
//gg_insert_into_assembler("# DUBNER addition END "); |
| 704 |
} |
| 705 |
return retval; |
| 706 |
} |
| 707 |
|
| 708 |
void |
| 709 |
parser_add( size_t nC, cbl_num_result_t *C, |
| 710 |
size_t nA, cbl_refer_t *A, |
| 711 |
cbl_arith_format_t format, |
| 712 |
cbl_label_t *error, |
| 713 |
cbl_label_t *not_error, |
| 714 |
void *compute_error_p ) // Cast this to a tree / int * |
| 715 |
{ |
| 716 |
Analyze(); |
| 717 |
SHOW_PARSE |
| 718 |
{ |
| 719 |
SHOW_PARSE_HEADER |
| 720 |
fprintf(stderr, " A[" HOST_SIZE_T_PRINT_DEC "]:", (fmt_size_t)nA); |
| 721 |
for(size_t i=0; i<nA; i++) |
| 722 |
{ |
| 723 |
if(i > 0) |
| 724 |
{ |
| 725 |
fprintf(stderr, ","); |
| 726 |
} |
| 727 |
fprintf(stderr, "%s", A[i].field->name); |
| 728 |
} |
| 729 |
|
| 730 |
fprintf(stderr, "%s", format==giving_e? " GIVING" : ""); |
| 731 |
|
| 732 |
fprintf(stderr, " C[" HOST_SIZE_T_PRINT_DEC "]:", (fmt_size_t)nC); |
| 733 |
for(size_t i=0; i<nC; i++) |
| 734 |
{ |
| 735 |
if(i > 0) |
| 736 |
{ |
| 737 |
fprintf(stderr, ","); |
| 738 |
} |
| 739 |
fprintf(stderr, "%s", C[i].refer.field->name); |
| 740 |
} |
| 741 |
|
| 742 |
SHOW_PARSE_END |
| 743 |
} |
| 744 |
|
| 745 |
TRACE1 |
| 746 |
{ |
| 747 |
TRACE1_HEADER |
| 748 |
TRACE1_END |
| 749 |
} |
| 750 |
|
| 751 |
bool handled = false; |
| 752 |
|
| 753 |
if( fast_add( nC, C, |
| 754 |
nA, A, |
| 755 |
format) ) |
| 756 |
{ |
| 757 |
handled = true; |
| 758 |
} |
| 759 |
else |
| 760 |
{ |
| 761 |
tree compute_error = (tree)compute_error_p; |
| 762 |
if( compute_error == NULL ) |
| 763 |
{ |
| 764 |
gg_assign(var_decl_default_compute_error, integer_zero_node); |
| 765 |
compute_error = gg_get_address_of(var_decl_default_compute_error); |
| 766 |
} |
| 767 |
|
| 768 |
bool computation_is_float = is_somebody_float(nA, A) |
| 769 |
|| is_somebody_float(nC, C); |
| 770 |
// We now start deciding which arithmetic routine we are going to use: |
| 771 |
if( computation_is_float ) |
| 772 |
{ |
| 773 |
switch( format ) |
| 774 |
{ |
| 775 |
case no_giving_e: |
| 776 |
{ |
| 777 |
// Float format 1 |
| 778 |
|
| 779 |
set_up_arithmetic_error_handler(error, |
| 780 |
not_error); |
| 781 |
// Do phase 1, which calculates the subtotal and puts it into a |
| 782 |
// temporary location |
| 783 |
arithmetic_operation( 0, NULL, |
| 784 |
nA, A, |
| 785 |
0, NULL, |
| 786 |
format, |
| 787 |
error, |
| 788 |
not_error, |
| 789 |
compute_error, |
| 790 |
"__gg__add_float_phase1"); |
| 791 |
|
| 792 |
// Do phase 2, which accumulates the subtotal into each target location in turn |
| 793 |
for(size_t i=0; i<nC; i++) |
| 794 |
{ |
| 795 |
arithmetic_operation(1, &C[i], |
| 796 |
0, NULL, |
| 797 |
0, NULL, |
| 798 |
format, |
| 799 |
error, |
| 800 |
not_error, |
| 801 |
compute_error, |
| 802 |
"__gg__addf1_float_phase2"); |
| 803 |
} |
| 804 |
arithmetic_error_handler( error, |
| 805 |
not_error, |
| 806 |
compute_error); |
| 807 |
|
| 808 |
handled = true; |
| 809 |
break; |
| 810 |
} |
| 811 |
|
| 812 |
case giving_e: |
| 813 |
{ |
| 814 |
// Float format 2 |
| 815 |
set_up_arithmetic_error_handler(error, |
| 816 |
not_error); |
| 817 |
// Do phase 1, which calculates the subtotal and puts it into a |
| 818 |
// temporary location |
| 819 |
arithmetic_operation( 0, NULL, |
| 820 |
nA, A, |
| 821 |
0, NULL, |
| 822 |
format, |
| 823 |
error, |
| 824 |
not_error, |
| 825 |
compute_error, |
| 826 |
"__gg__add_float_phase1"); |
| 827 |
|
| 828 |
// Do phase 2, which puts the subtotal into each target location in turn |
| 829 |
for(size_t i=0; i<nC; i++) |
| 830 |
{ |
| 831 |
arithmetic_operation(1, &C[i], |
| 832 |
0, NULL, |
| 833 |
0, NULL, |
| 834 |
format, |
| 835 |
error, |
| 836 |
not_error, |
| 837 |
compute_error, |
| 838 |
"__gg__float_phase2_assign_to_c"); |
| 839 |
} |
| 840 |
arithmetic_error_handler( error, |
| 841 |
not_error, |
| 842 |
compute_error); |
| 843 |
|
| 844 |
handled = true; |
| 845 |
break; |
| 846 |
} |
| 847 |
|
| 848 |
case corresponding_e: |
| 849 |
{ |
| 850 |
// Float format 3 |
| 851 |
gcc_assert(nA == nC); |
| 852 |
|
| 853 |
set_up_arithmetic_error_handler(error, |
| 854 |
not_error); |
| 855 |
arithmetic_operation(nC, C, |
| 856 |
nA, A, |
| 857 |
0, NULL, |
| 858 |
format, |
| 859 |
error, |
| 860 |
not_error, |
| 861 |
compute_error, |
| 862 |
"__gg__addf3"); |
| 863 |
arithmetic_error_handler( error, |
| 864 |
not_error, |
| 865 |
compute_error); |
| 866 |
|
| 867 |
handled = true; |
| 868 |
break; |
| 869 |
} |
| 870 |
|
| 871 |
case not_expected_e: |
| 872 |
gcc_unreachable(); |
| 873 |
break; |
| 874 |
} |
| 875 |
} |
| 876 |
else |
| 877 |
{ |
| 878 |
switch( format ) |
| 879 |
{ |
| 880 |
case no_giving_e: |
| 881 |
{ |
| 882 |
// Fixed format 1 |
| 883 |
|
| 884 |
set_up_arithmetic_error_handler(error, |
| 885 |
not_error); |
| 886 |
// Do phase 1, which calculates the subtotal and puts it into a |
| 887 |
// temporary location |
| 888 |
arithmetic_operation( 0, NULL, |
| 889 |
nA, A, |
| 890 |
0, NULL, |
| 891 |
format, |
| 892 |
error, |
| 893 |
not_error, |
| 894 |
compute_error, |
| 895 |
"__gg__add_fixed_phase1"); |
| 896 |
|
| 897 |
// Do phase 2, which accumulates the subtotal into each target location in turn |
| 898 |
for(size_t i=0; i<nC; i++) |
| 899 |
{ |
| 900 |
arithmetic_operation(1, &C[i], |
| 901 |
0, NULL, |
| 902 |
0, NULL, |
| 903 |
format, |
| 904 |
error, |
| 905 |
not_error, |
| 906 |
compute_error, |
| 907 |
"__gg__addf1_fixed_phase2"); |
| 908 |
} |
| 909 |
arithmetic_error_handler( error, |
| 910 |
not_error, |
| 911 |
compute_error); |
| 912 |
|
| 913 |
handled = true; |
| 914 |
break; |
| 915 |
} |
| 916 |
|
| 917 |
case giving_e: |
| 918 |
{ |
| 919 |
// Fixed format 2 |
| 920 |
|
| 921 |
set_up_arithmetic_error_handler(error, |
| 922 |
not_error); |
| 923 |
// Do phase 1, which calculates the subtotal and puts it into a |
| 924 |
// temporary location |
| 925 |
arithmetic_operation( 0, NULL, |
| 926 |
nA, A, |
| 927 |
0, NULL, |
| 928 |
format, |
| 929 |
error, |
| 930 |
not_error, |
| 931 |
compute_error, |
| 932 |
"__gg__add_fixed_phase1"); |
| 933 |
|
| 934 |
// Do phase 2, which puts the subtotal into each target location in turn |
| 935 |
for(size_t i=0; i<nC; i++) |
| 936 |
{ |
| 937 |
arithmetic_operation(1, &C[i], |
| 938 |
0, NULL, |
| 939 |
0, NULL, |
| 940 |
format, |
| 941 |
error, |
| 942 |
not_error, |
| 943 |
compute_error, |
| 944 |
"__gg__fixed_phase2_assign_to_c"); |
| 945 |
} |
| 946 |
arithmetic_error_handler( error, |
| 947 |
not_error, |
| 948 |
compute_error); |
| 949 |
|
| 950 |
handled = true; |
| 951 |
break; |
| 952 |
} |
| 953 |
|
| 954 |
case corresponding_e: |
| 955 |
{ |
| 956 |
// Fixed format 3 |
| 957 |
gcc_assert(nA == nC); |
| 958 |
|
| 959 |
set_up_arithmetic_error_handler(error, |
| 960 |
not_error); |
| 961 |
arithmetic_operation(nC, C, |
| 962 |
nA, A, |
| 963 |
0, NULL, |
| 964 |
format, |
| 965 |
error, |
| 966 |
not_error, |
| 967 |
compute_error, |
| 968 |
"__gg__addf3"); |
| 969 |
arithmetic_error_handler( error, |
| 970 |
not_error, |
| 971 |
compute_error); |
| 972 |
|
| 973 |
handled = true; |
| 974 |
break; |
| 975 |
} |
| 976 |
|
| 977 |
case not_expected_e: |
| 978 |
gcc_unreachable(); |
| 979 |
break; |
| 980 |
} |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
assert( handled ); |
| 985 |
} |
| 986 |
|
| 987 |
void |
| 988 |
parser_add( const cbl_refer_t& cref, |
| 989 |
const cbl_refer_t& aref, |
| 990 |
const cbl_refer_t& bref, |
| 991 |
cbl_round_t rounded) |
| 992 |
{ |
| 993 |
// This is the simple and innocent C = A + B |
| 994 |
cbl_num_result_t C[1]; |
| 995 |
C[0].rounded = rounded; |
| 996 |
C[0].refer = cref; |
| 997 |
|
| 998 |
cbl_refer_t A[2]; |
| 999 |
A[0] = aref; |
| 1000 |
A[1] = bref; |
| 1001 |
|
| 1002 |
parser_add( 1, C, |
| 1003 |
2, A, |
| 1004 |
giving_e, |
| 1005 |
NULL, |
| 1006 |
NULL ); |
| 1007 |
} |
| 1008 |
|
| 1009 |
void |
| 1010 |
parser_multiply(size_t nC, cbl_num_result_t *C, |
| 1011 |
size_t nA, cbl_refer_t *A, |
| 1012 |
size_t nB, cbl_refer_t *B, |
| 1013 |
cbl_label_t *error, |
| 1014 |
cbl_label_t *not_error, |
| 1015 |
void *compute_error_p ) // This is a pointer to an int |
| 1016 |
{ |
| 1017 |
Analyze(); |
| 1018 |
SHOW_PARSE |
| 1019 |
{ |
| 1020 |
SHOW_PARSE_HEADER |
| 1021 |
SHOW_PARSE_END |
| 1022 |
} |
| 1023 |
|
| 1024 |
if( fast_multiply(nC, C, |
| 1025 |
nA, A, |
| 1026 |
nB, B) ) |
| 1027 |
{ |
| 1028 |
|
| 1029 |
} |
| 1030 |
else |
| 1031 |
{ |
| 1032 |
tree compute_error = (tree)compute_error_p; |
| 1033 |
|
| 1034 |
if( compute_error == NULL ) |
| 1035 |
{ |
| 1036 |
gg_assign(var_decl_default_compute_error, integer_zero_node); |
| 1037 |
compute_error = gg_get_address_of(var_decl_default_compute_error); |
| 1038 |
} |
| 1039 |
|
| 1040 |
if( nB == 0 ) |
| 1041 |
{ |
| 1042 |
// This is a FORMAT 1 multiply |
| 1043 |
|
| 1044 |
set_up_arithmetic_error_handler(error, |
| 1045 |
not_error); |
| 1046 |
// Phase 1 just converts identifier 1 to its intermediate form |
| 1047 |
arithmetic_operation( 0, NULL, |
| 1048 |
nA, A, |
| 1049 |
0, NULL, |
| 1050 |
not_expected_e, |
| 1051 |
error, |
| 1052 |
not_error, |
| 1053 |
compute_error, |
| 1054 |
"__gg__multiplyf1_phase1"); |
| 1055 |
|
| 1056 |
// Phase2 multiplies the intermediate by each destination in turn |
| 1057 |
for(size_t i=0; i<nC; i++) |
| 1058 |
{ |
| 1059 |
arithmetic_operation( 1, &C[i], |
| 1060 |
0, NULL, |
| 1061 |
0, NULL, |
| 1062 |
not_expected_e, |
| 1063 |
error, |
| 1064 |
not_error, |
| 1065 |
compute_error, |
| 1066 |
"__gg__multiplyf1_phase2"); |
| 1067 |
} |
| 1068 |
arithmetic_error_handler( error, |
| 1069 |
not_error, |
| 1070 |
compute_error); |
| 1071 |
|
| 1072 |
} |
| 1073 |
else |
| 1074 |
{ |
| 1075 |
// This is a FORMAT 2 multiply |
| 1076 |
set_up_arithmetic_error_handler(error, |
| 1077 |
not_error); |
| 1078 |
arithmetic_operation( nC, C, |
| 1079 |
nA, A, |
| 1080 |
nB, B, |
| 1081 |
not_expected_e, |
| 1082 |
error, |
| 1083 |
not_error, |
| 1084 |
compute_error, |
| 1085 |
"__gg__multiplyf2"); |
| 1086 |
arithmetic_error_handler( error, |
| 1087 |
not_error, |
| 1088 |
compute_error); |
| 1089 |
} |
| 1090 |
} |
| 1091 |
TRACE1 |
| 1092 |
{ |
| 1093 |
TRACE1_HEADER |
| 1094 |
TRACE1_FIELD("result operand C[0]: ", C[0].refer.field, ""); |
| 1095 |
TRACE1_END |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
void |
| 1100 |
parser_divide( size_t nC, cbl_num_result_t *C, // C = A / B |
| 1101 |
size_t nA, cbl_refer_t *A, |
| 1102 |
size_t nB, cbl_refer_t *B, |
| 1103 |
cbl_refer_t remainder, |
| 1104 |
cbl_label_t *error, |
| 1105 |
cbl_label_t *not_error, |
| 1106 |
void *compute_error_p ) // This is a pointer to an int |
| 1107 |
{ |
| 1108 |
Analyze(); |
| 1109 |
SHOW_PARSE |
| 1110 |
{ |
| 1111 |
SHOW_PARSE_HEADER |
| 1112 |
SHOW_PARSE_END |
| 1113 |
} |
| 1114 |
|
| 1115 |
if( fast_divide(nC, C, |
| 1116 |
nA, A, |
| 1117 |
nB, B, |
| 1118 |
remainder) ) |
| 1119 |
{ |
| 1120 |
|
| 1121 |
} |
| 1122 |
else |
| 1123 |
{ |
| 1124 |
tree compute_error = (tree)compute_error_p; |
| 1125 |
|
| 1126 |
if( compute_error == NULL ) |
| 1127 |
{ |
| 1128 |
gg_assign(var_decl_default_compute_error, integer_zero_node); |
| 1129 |
compute_error = gg_get_address_of(var_decl_default_compute_error); |
| 1130 |
} |
| 1131 |
|
| 1132 |
if( nB == 0 && !remainder.field ) |
| 1133 |
{ |
| 1134 |
// This is a format 1 division |
| 1135 |
set_up_arithmetic_error_handler(error, |
| 1136 |
not_error); |
| 1137 |
arithmetic_operation(0, NULL, |
| 1138 |
nA, A, |
| 1139 |
0, NULL, |
| 1140 |
not_expected_e, |
| 1141 |
NULL, |
| 1142 |
NULL, |
| 1143 |
compute_error, |
| 1144 |
"__gg__multiplyf1_phase1"); |
| 1145 |
|
| 1146 |
for(size_t i=0; i<nC; i++) |
| 1147 |
{ |
| 1148 |
arithmetic_operation(1, &C[i], |
| 1149 |
0, NULL, |
| 1150 |
0, NULL, |
| 1151 |
not_expected_e, |
| 1152 |
error, |
| 1153 |
not_error, |
| 1154 |
compute_error, |
| 1155 |
"__gg__dividef1_phase2"); |
| 1156 |
} |
| 1157 |
arithmetic_error_handler( error, |
| 1158 |
not_error, |
| 1159 |
compute_error); |
| 1160 |
} |
| 1161 |
|
| 1162 |
if( nB && !remainder.field ) |
| 1163 |
{ |
| 1164 |
// This is a format 2/3 division |
| 1165 |
set_up_arithmetic_error_handler(error, |
| 1166 |
not_error); |
| 1167 |
arithmetic_operation(nC, C, |
| 1168 |
1, A, |
| 1169 |
1, B, |
| 1170 |
not_expected_e, |
| 1171 |
error, |
| 1172 |
not_error, |
| 1173 |
compute_error, |
| 1174 |
"__gg__dividef23"); |
| 1175 |
|
| 1176 |
arithmetic_error_handler( error, |
| 1177 |
not_error, |
| 1178 |
compute_error); |
| 1179 |
} |
| 1180 |
|
| 1181 |
if( remainder.field ) |
| 1182 |
{ |
| 1183 |
// This is a format 4/5 division |
| 1184 |
set_up_arithmetic_error_handler(error, |
| 1185 |
not_error); |
| 1186 |
arithmetic_operation(1, C, |
| 1187 |
1, A, |
| 1188 |
1, B, |
| 1189 |
not_expected_e, |
| 1190 |
error, |
| 1191 |
not_error, |
| 1192 |
compute_error, |
| 1193 |
"__gg__dividef45", |
| 1194 |
&remainder); |
| 1195 |
|
| 1196 |
arithmetic_error_handler( error, |
| 1197 |
not_error, |
| 1198 |
compute_error); |
| 1199 |
} |
| 1200 |
} |
| 1201 |
|
| 1202 |
TRACE1 |
| 1203 |
{ |
| 1204 |
TRACE1_HEADER |
| 1205 |
TRACE1_END |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
void |
| 1210 |
parser_multiply(const cbl_refer_t& cref, |
| 1211 |
const cbl_refer_t& aref, |
| 1212 |
const cbl_refer_t& bref, |
| 1213 |
cbl_round_t rounded ) |
| 1214 |
{ |
| 1215 |
cbl_num_result_t C[1]; |
| 1216 |
C[0].rounded = rounded; |
| 1217 |
C[0].refer = cref; |
| 1218 |
|
| 1219 |
cbl_refer_t A[1]; |
| 1220 |
A[0] = aref; |
| 1221 |
|
| 1222 |
cbl_refer_t B[1]; |
| 1223 |
B[0] = bref; |
| 1224 |
|
| 1225 |
parser_multiply(1, C, |
| 1226 |
1, B, |
| 1227 |
1, A, |
| 1228 |
NULL, |
| 1229 |
NULL ); |
| 1230 |
} |
| 1231 |
|
| 1232 |
void |
| 1233 |
parser_divide( const cbl_refer_t& cref, |
| 1234 |
const cbl_refer_t& aref, |
| 1235 |
const cbl_refer_t& bref, |
| 1236 |
cbl_round_t rounded, |
| 1237 |
const cbl_refer_t& remainder_ref ) |
| 1238 |
{ |
| 1239 |
cbl_num_result_t C[1]; |
| 1240 |
C[0].rounded = rounded; |
| 1241 |
C[0].refer = cref; |
| 1242 |
|
| 1243 |
cbl_refer_t A[1]; |
| 1244 |
A[0] = aref; |
| 1245 |
|
| 1246 |
cbl_refer_t B[1]; |
| 1247 |
B[0] = bref; |
| 1248 |
|
| 1249 |
parser_divide( 1, C, |
| 1250 |
1, A, |
| 1251 |
1, B, |
| 1252 |
remainder_ref, |
| 1253 |
NULL, |
| 1254 |
NULL ); |
| 1255 |
} |
| 1256 |
|
| 1257 |
void |
| 1258 |
parser_op( struct cbl_refer_t cref, |
| 1259 |
struct cbl_refer_t aref, |
| 1260 |
int op, |
| 1261 |
struct cbl_refer_t bref, |
| 1262 |
struct cbl_label_t *compute_error_label) |
| 1263 |
{ |
| 1264 |
Analyze(); |
| 1265 |
set_up_compute_error_label(compute_error_label); |
| 1266 |
|
| 1267 |
gg_assign(var_decl_default_compute_error, integer_zero_node); |
| 1268 |
tree compute_error = compute_error_label |
| 1269 |
? gg_get_address_of( compute_error_label-> |
| 1270 |
structs.compute_error-> |
| 1271 |
compute_error_code) |
| 1272 |
: gg_get_address_of(var_decl_default_compute_error) ; |
| 1273 |
SHOW_PARSE |
| 1274 |
{ |
| 1275 |
SHOW_PARSE_HEADER |
| 1276 |
SHOW_PARSE_REF(" ", cref) |
| 1277 |
SHOW_PARSE_TEXT(" = ") |
| 1278 |
SHOW_PARSE_REF("", aref) |
| 1279 |
char ach[4] = " "; |
| 1280 |
ach[1] = op; |
| 1281 |
SHOW_PARSE_TEXT(ach); |
| 1282 |
SHOW_PARSE_REF("", bref) |
| 1283 |
SHOW_PARSE_END |
| 1284 |
} |
| 1285 |
|
| 1286 |
// We have to do the trace in before/after mode; parser_op(a, a, op, a) |
| 1287 |
// is a legitimate call. |
| 1288 |
TRACE1 |
| 1289 |
{ |
| 1290 |
TRACE1_HEADER |
| 1291 |
char ach[4] = " "; |
| 1292 |
ach[1] = op; |
| 1293 |
TRACE1_TEXT_ABC("operation is \"", ach, "\"") |
| 1294 |
TRACE1_INDENT |
| 1295 |
TRACE1_REFER("operand A: ", aref, "") |
| 1296 |
TRACE1_INDENT |
| 1297 |
TRACE1_REFER("operand B: ", bref, "") |
| 1298 |
TRACE1_INDENT |
| 1299 |
TRACE1_TEXT_ABC("result will be ", cref.field->name, "") |
| 1300 |
TRACE1_END |
| 1301 |
} |
| 1302 |
|
| 1303 |
struct cbl_num_result_t for_call = {}; |
| 1304 |
for_call.rounded = truncation_e; |
| 1305 |
for_call.refer = cref; |
| 1306 |
|
| 1307 |
switch(op) |
| 1308 |
{ |
| 1309 |
case '+': |
| 1310 |
{ |
| 1311 |
cbl_refer_t A[2]; |
| 1312 |
A[0] = aref; |
| 1313 |
A[1] = bref; |
| 1314 |
parser_add( 1, &for_call, |
| 1315 |
2, A, |
| 1316 |
giving_e, |
| 1317 |
NULL, |
| 1318 |
NULL, |
| 1319 |
compute_error ); |
| 1320 |
break; |
| 1321 |
} |
| 1322 |
|
| 1323 |
case '-': |
| 1324 |
{ |
| 1325 |
cbl_refer_t A[1]; |
| 1326 |
cbl_refer_t B[1]; |
| 1327 |
A[0] = bref; |
| 1328 |
B[0] = aref; |
| 1329 |
// Yes, the A-ness and B-ness are not really consistent |
| 1330 |
parser_subtract(1, &for_call, |
| 1331 |
1, A, |
| 1332 |
1, B, |
| 1333 |
giving_e, |
| 1334 |
NULL, |
| 1335 |
NULL, |
| 1336 |
compute_error ); |
| 1337 |
break; |
| 1338 |
} |
| 1339 |
|
| 1340 |
case '*': |
| 1341 |
{ |
| 1342 |
cbl_refer_t A[1]; |
| 1343 |
cbl_refer_t B[1]; |
| 1344 |
A[0] = bref; |
| 1345 |
B[0] = aref; |
| 1346 |
parser_multiply(1, &for_call, |
| 1347 |
1, A, |
| 1348 |
1, B, |
| 1349 |
NULL, |
| 1350 |
NULL, |
| 1351 |
compute_error ); |
| 1352 |
break; |
| 1353 |
} |
| 1354 |
|
| 1355 |
case '/': |
| 1356 |
{ |
| 1357 |
cbl_refer_t A[1]; |
| 1358 |
cbl_refer_t B[1]; |
| 1359 |
A[0] = aref; |
| 1360 |
B[0] = bref; |
| 1361 |
parser_divide(1, &for_call, |
| 1362 |
1, A, |
| 1363 |
1, B, |
| 1364 |
NULL, |
| 1365 |
NULL, |
| 1366 |
NULL, |
| 1367 |
compute_error ); |
| 1368 |
break; |
| 1369 |
} |
| 1370 |
|
| 1371 |
case '^': |
| 1372 |
{ |
| 1373 |
arithmetic_operation( 1, &for_call, |
| 1374 |
1, &aref, |
| 1375 |
1, &bref, |
| 1376 |
no_giving_e, |
| 1377 |
NULL, |
| 1378 |
NULL, |
| 1379 |
compute_error, |
| 1380 |
"__gg__pow", |
| 1381 |
NULL); |
| 1382 |
break; |
| 1383 |
} |
| 1384 |
default: |
| 1385 |
cbl_internal_error( "%<parser_op()%> doesn%'t know how to " |
| 1386 |
"evaluate %<%s = %s %c %s%>", |
| 1387 |
cref.field->name, |
| 1388 |
aref.field->name, |
| 1389 |
op, |
| 1390 |
bref.field->name); |
| 1391 |
break; |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
void |
| 1396 |
parser_subtract(size_t nC, cbl_num_result_t *C, // C = B - A |
| 1397 |
size_t nA, cbl_refer_t *A, |
| 1398 |
size_t nB, cbl_refer_t *B, |
| 1399 |
cbl_arith_format_t format, |
| 1400 |
cbl_label_t *error, |
| 1401 |
cbl_label_t *not_error, |
| 1402 |
void *compute_error_p ) // Cast this to a tree / int * |
| 1403 |
{ |
| 1404 |
Analyze(); |
| 1405 |
SHOW_PARSE |
| 1406 |
{ |
| 1407 |
SHOW_PARSE_HEADER |
| 1408 |
fprintf(stderr, " A[" HOST_SIZE_T_PRINT_DEC "]:", (fmt_size_t)nA); |
| 1409 |
for(size_t i=0; i<nA; i++) |
| 1410 |
{ |
| 1411 |
if(i > 0) |
| 1412 |
{ |
| 1413 |
fprintf(stderr, ","); |
| 1414 |
} |
| 1415 |
fprintf(stderr, "%s", A[i].field->name); |
| 1416 |
} |
| 1417 |
|
| 1418 |
fprintf(stderr, " B[" HOST_SIZE_T_PRINT_DEC "]:", (fmt_size_t)nB); |
| 1419 |
for(size_t i=0; i<nB; i++) |
| 1420 |
{ |
| 1421 |
if(i > 0) |
| 1422 |
{ |
| 1423 |
fprintf(stderr, ","); |
| 1424 |
} |
| 1425 |
fprintf(stderr, "%s", B[i].field->name); |
| 1426 |
} |
| 1427 |
|
| 1428 |
fprintf(stderr, " C[" HOST_SIZE_T_PRINT_DEC "]:", (fmt_size_t)nC); |
| 1429 |
for(size_t i=0; i<nC; i++) |
| 1430 |
{ |
| 1431 |
if(i > 0) |
| 1432 |
{ |
| 1433 |
fprintf(stderr, ","); |
| 1434 |
} |
| 1435 |
fprintf(stderr, "%s", C[i].refer.field->name); |
| 1436 |
} |
| 1437 |
|
| 1438 |
SHOW_PARSE_END |
| 1439 |
} |
| 1440 |
|
| 1441 |
// We are going to look for configurations that allow us to do binary |
| 1442 |
// arithmetic and quickly assign the results: |
| 1443 |
|
| 1444 |
// no_giving_e is format 1; giving_e is format 2. |
| 1445 |
|
| 1446 |
bool handled = false; |
| 1447 |
|
| 1448 |
if( fast_subtract(nC, C, |
| 1449 |
nA, A, |
| 1450 |
nB, B, |
| 1451 |
format) ) |
| 1452 |
{ |
| 1453 |
handled = true; |
| 1454 |
} |
| 1455 |
else |
| 1456 |
{ |
| 1457 |
tree compute_error = (tree)compute_error_p; |
| 1458 |
if( compute_error == NULL ) |
| 1459 |
{ |
| 1460 |
gg_assign(var_decl_default_compute_error, integer_zero_node); |
| 1461 |
compute_error = gg_get_address_of(var_decl_default_compute_error); |
| 1462 |
} |
| 1463 |
bool computation_is_float = is_somebody_float(nA, A) |
| 1464 |
|| is_somebody_float(nC, C); |
| 1465 |
|
| 1466 |
// We now start deciding which arithmetic routine we are going to use: |
| 1467 |
|
| 1468 |
if( computation_is_float ) |
| 1469 |
{ |
| 1470 |
switch( format ) |
| 1471 |
{ |
| 1472 |
case no_giving_e: |
| 1473 |
{ |
| 1474 |
// Float format 1 |
| 1475 |
|
| 1476 |
set_up_arithmetic_error_handler(error, |
| 1477 |
not_error); |
| 1478 |
// Do phase 1, which calculates the subtotal and puts it into a |
| 1479 |
// temporary location |
| 1480 |
arithmetic_operation( 0, NULL, |
| 1481 |
nA, A, |
| 1482 |
0, NULL, |
| 1483 |
format, |
| 1484 |
error, |
| 1485 |
not_error, |
| 1486 |
compute_error, |
| 1487 |
"__gg__add_float_phase1"); |
| 1488 |
|
| 1489 |
// Do phase 2, which subtracts the subtotal from each target in turn |
| 1490 |
for(size_t i=0; i<nC; i++) |
| 1491 |
{ |
| 1492 |
arithmetic_operation(1, &C[i], |
| 1493 |
0, NULL, |
| 1494 |
0, NULL, |
| 1495 |
format, |
| 1496 |
error, |
| 1497 |
not_error, |
| 1498 |
compute_error, |
| 1499 |
"__gg__subtractf1_float_phase2"); |
| 1500 |
} |
| 1501 |
arithmetic_error_handler( error, |
| 1502 |
not_error, |
| 1503 |
compute_error); |
| 1504 |
|
| 1505 |
handled = true; |
| 1506 |
|
| 1507 |
break; |
| 1508 |
} |
| 1509 |
|
| 1510 |
case giving_e: |
| 1511 |
{ |
| 1512 |
// Float SUBTRACT Format 2 |
| 1513 |
|
| 1514 |
gcc_assert(nB == 1); |
| 1515 |
set_up_arithmetic_error_handler(error, |
| 1516 |
not_error); |
| 1517 |
// Do phase 1, which calculates the subtotal and puts it into a |
| 1518 |
// temporary location |
| 1519 |
arithmetic_operation( 0, NULL, |
| 1520 |
nA, A, |
| 1521 |
nB, B, |
| 1522 |
format, |
| 1523 |
error, |
| 1524 |
not_error, |
| 1525 |
compute_error, |
| 1526 |
"__gg__subtractf2_float_phase1"); |
| 1527 |
|
| 1528 |
// Do phase 2, which puts the subtotal into each target location in turn |
| 1529 |
for(size_t i=0; i<nC; i++) |
| 1530 |
{ |
| 1531 |
arithmetic_operation(1, &C[i], |
| 1532 |
0, NULL, |
| 1533 |
0, NULL, |
| 1534 |
format, |
| 1535 |
error, |
| 1536 |
not_error, |
| 1537 |
compute_error, |
| 1538 |
"__gg__fixed_phase2_assign_to_c"); |
| 1539 |
} |
| 1540 |
arithmetic_error_handler( error, |
| 1541 |
not_error, |
| 1542 |
compute_error); |
| 1543 |
|
| 1544 |
handled = true; |
| 1545 |
break; |
| 1546 |
} |
| 1547 |
|
| 1548 |
case corresponding_e: |
| 1549 |
{ |
| 1550 |
// Float format 3 |
| 1551 |
gcc_assert(nA == nC); |
| 1552 |
|
| 1553 |
set_up_arithmetic_error_handler(error, |
| 1554 |
not_error); |
| 1555 |
arithmetic_operation(nC, C, |
| 1556 |
nA, A, |
| 1557 |
0, NULL, |
| 1558 |
format, |
| 1559 |
error, |
| 1560 |
not_error, |
| 1561 |
compute_error, |
| 1562 |
"__gg__subtractf3"); |
| 1563 |
arithmetic_error_handler( error, |
| 1564 |
not_error, |
| 1565 |
compute_error); |
| 1566 |
|
| 1567 |
handled = true; |
| 1568 |
|
| 1569 |
break; |
| 1570 |
} |
| 1571 |
|
| 1572 |
case not_expected_e: |
| 1573 |
gcc_unreachable(); |
| 1574 |
break; |
| 1575 |
} |
| 1576 |
} |
| 1577 |
else |
| 1578 |
{ |
| 1579 |
switch( format ) |
| 1580 |
{ |
| 1581 |
case no_giving_e: |
| 1582 |
{ |
| 1583 |
// Fixed format 1 |
| 1584 |
|
| 1585 |
set_up_arithmetic_error_handler(error, |
| 1586 |
not_error); |
| 1587 |
// Do phase 1, which calculates the subtotal and puts it into a |
| 1588 |
// temporary location |
| 1589 |
arithmetic_operation( 0, NULL, |
| 1590 |
nA, A, |
| 1591 |
0, NULL, |
| 1592 |
format, |
| 1593 |
error, |
| 1594 |
not_error, |
| 1595 |
compute_error, |
| 1596 |
"__gg__add_fixed_phase1"); |
| 1597 |
|
| 1598 |
// Do phase 2, which subtracts the subtotal from each target in turn |
| 1599 |
for(size_t i=0; i<nC; i++) |
| 1600 |
{ |
| 1601 |
arithmetic_operation(1, &C[i], |
| 1602 |
0, NULL, |
| 1603 |
0, NULL, |
| 1604 |
format, |
| 1605 |
error, |
| 1606 |
not_error, |
| 1607 |
compute_error, |
| 1608 |
"__gg__subtractf1_fixed_phase2"); |
| 1609 |
} |
| 1610 |
arithmetic_error_handler( error, |
| 1611 |
not_error, |
| 1612 |
compute_error); |
| 1613 |
|
| 1614 |
handled = true; |
| 1615 |
|
| 1616 |
break; |
| 1617 |
} |
| 1618 |
|
| 1619 |
case giving_e: |
| 1620 |
{ |
| 1621 |
// Fixed SUBTRACT Format 2 |
| 1622 |
|
| 1623 |
gcc_assert(nB == 1); |
| 1624 |
set_up_arithmetic_error_handler(error, |
| 1625 |
not_error); |
| 1626 |
// Do phase 1, which calculates the subtotal and puts it into a |
| 1627 |
// temporary location |
| 1628 |
arithmetic_operation( 0, NULL, |
| 1629 |
nA, A, |
| 1630 |
nB, B, |
| 1631 |
format, |
| 1632 |
error, |
| 1633 |
not_error, |
| 1634 |
compute_error, |
| 1635 |
"__gg__subtractf2_fixed_phase1"); |
| 1636 |
|
| 1637 |
// Do phase 2, which puts the subtotal into each target location in turn |
| 1638 |
for(size_t i=0; i<nC; i++) |
| 1639 |
{ |
| 1640 |
arithmetic_operation( 1, &C[i], |
| 1641 |
0, NULL, |
| 1642 |
0, NULL, |
| 1643 |
format, |
| 1644 |
error, |
| 1645 |
not_error, |
| 1646 |
compute_error, |
| 1647 |
"__gg__fixed_phase2_assign_to_c"); |
| 1648 |
} |
| 1649 |
arithmetic_error_handler( error, |
| 1650 |
not_error, |
| 1651 |
compute_error); |
| 1652 |
|
| 1653 |
handled = true; |
| 1654 |
break; |
| 1655 |
} |
| 1656 |
|
| 1657 |
case corresponding_e: |
| 1658 |
{ |
| 1659 |
// Fixed format 3 |
| 1660 |
gcc_assert(nA == nC); |
| 1661 |
|
| 1662 |
set_up_arithmetic_error_handler(error, |
| 1663 |
not_error); |
| 1664 |
arithmetic_operation(nC, C, |
| 1665 |
nA, A, |
| 1666 |
0, NULL, |
| 1667 |
format, |
| 1668 |
error, |
| 1669 |
not_error, |
| 1670 |
compute_error, |
| 1671 |
"__gg__subtractf3"); |
| 1672 |
arithmetic_error_handler( error, |
| 1673 |
not_error, |
| 1674 |
compute_error); |
| 1675 |
|
| 1676 |
handled = true; |
| 1677 |
break; |
| 1678 |
} |
| 1679 |
|
| 1680 |
case not_expected_e: |
| 1681 |
gcc_unreachable(); |
| 1682 |
break; |
| 1683 |
} |
| 1684 |
} |
| 1685 |
} |
| 1686 |
|
| 1687 |
if( !handled ) |
| 1688 |
{ |
| 1689 |
abort(); |
| 1690 |
} |
| 1691 |
TRACE1 |
| 1692 |
{ |
| 1693 |
TRACE1_HEADER |
| 1694 |
TRACE1_END |
| 1695 |
} |
| 1696 |
} |
| 1697 |
|
| 1698 |
void |
| 1699 |
parser_subtract(const cbl_refer_t& cref, // cref = aref - bref |
| 1700 |
const cbl_refer_t& aref, |
| 1701 |
const cbl_refer_t& bref, |
| 1702 |
cbl_round_t rounded ) |
| 1703 |
{ |
| 1704 |
cbl_num_result_t C[1]; |
| 1705 |
C[0].rounded = rounded; |
| 1706 |
C[0].refer = cref; |
| 1707 |
|
| 1708 |
cbl_refer_t A[1]; |
| 1709 |
A[0] = aref; |
| 1710 |
|
| 1711 |
cbl_refer_t B[1]; |
| 1712 |
B[0] = bref; |
| 1713 |
|
| 1714 |
parser_subtract(1, C, // Beware: C = A - B, but the order has changed |
| 1715 |
1, B, |
| 1716 |
1, A, |
| 1717 |
giving_e, |
| 1718 |
NULL, |
| 1719 |
NULL ); |
| 1720 |
} |