`EXPR` is the core function use by the code generator to generate C-functions. It encapsulates the expression to execute by the virtual machine, for each block. Parameters ---------- opNum The index in the operation table, which is finalized as a C `switch-case`
(opNum: int, expr: str, retChar: str,
arg1Dchar: str=None, arg2Dchar: str=None, arg3Dchar: str=None, vecType: int=TYPE_LOOP)
| 519 | TYPE_ALIGNED = 2 |
| 520 | TYPE_STRIDED = 3 |
| 521 | def EXPR(opNum: int, expr: str, retChar: str, |
| 522 | arg1Dchar: str=None, arg2Dchar: str=None, arg3Dchar: str=None, vecType: int=TYPE_LOOP) -> str: |
| 523 | ''' |
| 524 | `EXPR` is the core function use by the code generator to generate C-functions. |
| 525 | It encapsulates the expression to execute by the virtual machine, for each |
| 526 | block. |
| 527 | |
| 528 | Parameters |
| 529 | ---------- |
| 530 | opNum |
| 531 | The index in the operation table, which is finalized as a C `switch-case` |
| 532 | statement block. |
| 533 | expr |
| 534 | A string expression with variables (all of which start with ``$``) |
| 535 | |
| 536 | vecType |
| 537 | The vectorization enumerator (presently one of ``{TYPE_LOOP, TYPE_ALIGNED, TYPE_STRIDED}``). |
| 538 | Returns |
| 539 | ------- |
| 540 | expr |
| 541 | |
| 542 | ''' |
| 543 | argChars = [item for item in (arg1Dchar, arg2Dchar, arg3Dchar) if item != None] |
| 544 | |
| 545 | # STRING, and UNICODE have special operation syntax |
| 546 | # Perhaps it would be easier to make string operations _all_ functions. |
| 547 | if 'S' in argChars or 'U' in argChars: |
| 548 | print( 'TODO: string: %s ' % expr ) |
| 549 | return |
| 550 | |
| 551 | # Replace tokens |
| 552 | # TODO: this should moreso be a C-define, but perhaps BLOCK_SIZE1/2/4/8/16? |
| 553 | expr = expr.replace( '$BLOCKSIZE', str(BLOCKSIZE) ) |
| 554 | |
| 555 | # Build loop structure |
| 556 | # docString = '// {} ({}) :: {}'.format(retChar, argChars, expr) |
| 557 | if vecType == TYPE_LOOP: |
| 558 | expr = VEC_ARGN[len(argChars)](expr) |
| 559 | #expr = 'case {0}: {2} {1}'.format( opNum, VEC_ARGN[len(argChars)](expr), docString ) |
| 560 | elif vecType == TYPE_ALIGNED: |
| 561 | expr = VEC_ARGN_ALIGNED[len(argChars)](expr) |
| 562 | #expr = 'case {0}: {2} {1}'.format( opNum, VEC_ARGN_ALIGNED[len(argChars)](expr), docString ) |
| 563 | elif vecType == TYPE_STRIDED: |
| 564 | expr = VEC_ARGN_STRIDED[len(argChars)](expr) |
| 565 | #expr = 'case {0}: {2} {1}'.format( opNum, VEC_ARGN_STRIDED[len(argChars)](expr), docString ) |
| 566 | else: |
| 567 | raise ValueError( "Unknown vectorization type: {}".format(vecType) ) |
| 568 | |
| 569 | expr = expr.replace( '$DEST', DEST() ) |
| 570 | expr = expr.replace( '$DTYPE0', DCHAR_TO_CTYPE[retChar] ) |
| 571 | for I, dchar in enumerate(argChars): |
| 572 | #expr = expr.replace( '$ARG{}'.format(I+1), ARG( dchar, I+1 ) ) |
| 573 | expr = expr.replace( '$DTYPE{}'.format(I+1), DCHAR_TO_CTYPE[dchar] ) |
| 574 | |
| 575 | return expr |
| 576 | |
| 577 | |
| 578 |