jit_blocks 0.1.0
Loading...
Searching...
No Matches
Data Structures | Typedefs | Functions
Dynamic Switch Builder

Builds a function acting as a big dynamic switch like below: More...

Data Structures

struct  jit_blocks_dynswitch_cond_t
 A condition for the dynamic switch. More...
 

Typedefs

typedef void(* jit_blocks_dynswitch_func_ptr_t) (void *ctx, long val)
 The function pointer type called in each condition.
 
typedef struct jit_blocks_dynswitch_cond_t jit_blocks_dynswitch_cond_t
 A condition for the dynamic switch.
 
typedef jit_blocks_dynswitch_func_ptr_t jit_blocks_dynswitch_result_t
 The output function pointer type.
 

Functions

JIT_BLOCKS_EXPORT jit_blocks_dynswitch_result_t jit_blocks_dynswitch_build (const jit_blocks_dynswitch_cond_t *conds, int num_conds, jit_blocks_dynswitch_func_ptr_t default_func, gcc_jit_result **out_res)
 Builds a function that performs a dynamic switch.
 
JIT_BLOCKS_EXPORT jit_blocks_dynswitch_result_t jit_blocks_dynswitch_build_aux (const jit_blocks_dynswitch_cond_t *conds, int num_conds, jit_blocks_dynswitch_func_ptr_t default_func, gcc_jit_context *ctx, gcc_jit_result **out_res)
 

Detailed Description

Builds a function acting as a big dynamic switch like below:

Given the below construction call:

{.val = config_val_0, .func = func_ptr_0},
{.val = config_val_1, .func = func_ptr_1},
};
conds,
2,
func_ptr_default);
JIT_BLOCKS_EXPORT jit_blocks_dynswitch_result_t jit_blocks_dynswitch_build(const jit_blocks_dynswitch_cond_t *conds, int num_conds, jit_blocks_dynswitch_func_ptr_t default_func, gcc_jit_result **out_res)
Builds a function that performs a dynamic switch.
jit_blocks_dynswitch_func_ptr_t jit_blocks_dynswitch_result_t
The output function pointer type.
Definition jit_blocks.h:302
A condition for the dynamic switch.
Definition jit_blocks.h:296
long val
Definition jit_blocks.h:297

It generates the following function:

void output(void* ctx, long val) {
switch (val) {
case config_val_0:
func_ptr_0(ctx, val);
break;
case config_val_1:
func_ptr_1(ctx, val);
break;
default:
func_ptr_default(ctx, val);
break;
}
}

This could be used as an alternative to computed goto if the condition table is only known at run-time (e.g., build a dispatch() function of a dynamic base method that supports a dynamic dispatch table). Otherwise, computed goto might be a better choice.

Typedef Documentation

◆ jit_blocks_dynswitch_cond_t

A condition for the dynamic switch.

◆ jit_blocks_dynswitch_func_ptr_t

typedef void(* jit_blocks_dynswitch_func_ptr_t) (void *ctx, long val)

The function pointer type called in each condition.

◆ jit_blocks_dynswitch_result_t

The output function pointer type.

Function Documentation

◆ jit_blocks_dynswitch_build()

JIT_BLOCKS_EXPORT jit_blocks_dynswitch_result_t jit_blocks_dynswitch_build ( const jit_blocks_dynswitch_cond_t conds,
int  num_conds,
jit_blocks_dynswitch_func_ptr_t  default_func,
gcc_jit_result **  out_res 
)

Builds a function that performs a dynamic switch.

It matches conds array in order and calls the corresponding function. If no match is found, it calls default_func.

Note
the val in each cond must not be duplicated.

◆ jit_blocks_dynswitch_build_aux()

JIT_BLOCKS_EXPORT jit_blocks_dynswitch_result_t jit_blocks_dynswitch_build_aux ( const jit_blocks_dynswitch_cond_t conds,
int  num_conds,
jit_blocks_dynswitch_func_ptr_t  default_func,
gcc_jit_context *  ctx,
gcc_jit_result **  out_res 
)