COD (C-On-Demand) Dynamic Code Generation
Data Structures | Defines | Typedefs | Functions
cod.h File Reference

Go to the source code of this file.

Data Structures

struct  _FMField
struct  _FMOptInfo
struct  _FMformat_list
struct  extern_entry
struct  _cod_code_struct

Defines

#define FMOffset(p_type, field)   ((int) (((char *) (&(((p_type)0)->field))) - ((char *) 0)))
#define FMstr(s)   "s"
#define FMArrayDecl(type, size)   FMstr(type[size])
#define FMArrayDecl2(type, size, size2)   FMstr(type[size][size2])
#define FMDefaultDecl(name, val)   FMstr(name(val))

Typedefs

typedef struct parse_struct * cod_parse_context
typedef struct cod_exec_struct * cod_exec_context
typedef struct sm_struct * sm_ref
typedef struct _FMField FMField
typedef struct _FMFieldFMFieldList
typedef struct _FMOptInfo FMOptInfo
typedef struct _FMformat_list FMStructDescRec
typedef struct _FMformat_listFMStructDescList
typedef struct extern_entry cod_extern_entry
typedef cod_extern_entrycod_extern_list
typedef struct _cod_code_structcod_code
typedef void(* err_out_func_t )(void *client_data, char *string)

Functions

cod_parse_context new_cod_parse_context (void)
void cod_free_parse_context (cod_parse_context context)
void cod_assoc_externs (cod_parse_context context, cod_extern_list externs)
void cod_subroutine_declaration (const char *decl, cod_parse_context context)
 This is used to establish the parameter profile and return type of the subroutine to be generated.
void cod_add_simple_struct_type (const char *name, FMFieldList field_list, cod_parse_context context)
 Make a structured type, represented by a FFS-style FMFieldList, available in a particular context.
void cod_add_struct_type (FMStructDescList format_list, cod_parse_context context)
 Make a set of structured types, represented by a FFS-style FMStructDescList, available in a particular context.
void cod_add_param (const char *id, const char *typ, int param_num, cod_parse_context context)
 As an alternative to cod_subroutine_declaration(), add declaration for the Nth parameter.
void cod_set_return_type (char *typ, cod_parse_context context)
 add a declaration for the Nth parameter as an FFS-encoded structure
cod_code cod_code_gen (char *code, cod_parse_context context)
int cod_code_verify (char *code, cod_parse_context context)
void cod_code_free (cod_code code)
cod_exec_context cod_create_exec_context (cod_code code)
void cod_exec_context_free (cod_exec_context ec)
void cod_assoc_client_data (cod_exec_context ec, int key, long value)
long cod_get_client_data (cod_exec_context ec, int key)
void * cod_extract_state (cod_exec_context ec, int *length_p)
int cod_install_state (cod_exec_context ec, void *state, int length)
int cod_parse_for_context (char *code, cod_parse_context context)
 This parses a string to determine what external functions and variables are visible during code generation.
int cod_parse_for_globals (char *code, cod_parse_context context)
 This parses a string to setup global variables that are visible during code generation.
cod_parse_context cod_copy_context (cod_parse_context context)
cod_parse_context cod_copy_globals (cod_parse_context context)
void cod_set_error_func (cod_parse_context context, err_out_func_t err_func)
void cod_set_dont_coerce_return (cod_parse_context context, int value)
void cod_dump (cod_code code)
cod_code gen_rollback_code (FMStructDescList format1, FMStructDescList format2, char *xform_code)
void cod_add_int_constant_to_parse_context (const char *id, int value, cod_parse_context context)
 This is used to add an integer constant available in a particular context.
void cod_set_closure (char *name, void *value, cod_parse_context context)
 Sets what value cod_closure_context will send to a subroutine.

Detailed Description


Typedef Documentation

typedef struct parse_struct* cod_parse_context

cod_parse_context is the basic handle controlling generation of subroutines and functions. It is used in nearly all calls to setup parameter profiles and other information and to generate the code. Using a separate handle rather than static variables to store this information over the multiple calls needed for code generation ensures thread safety.

typedef struct cod_exec_struct* cod_exec_context

cod_exec_context is used to provide an execution context for COD-generated procedures. The full set of uses for this have not been determined, but it will support execution emulation on platforms where we do not have dynamic code generation, extraction of static state from functions, and the ability to pass information "through" a generated function from the calling context and make it available to a subroutine that might be called by the generated code.

typedef struct sm_struct* sm_ref

sm_ref is a pointer to an internal COD data structure representing a semantic element of a program fragment (E.G. a parameter definition).

A structure to hold Format Name / Field List associations.

This is used to associate names with field lists. Together these define a structure that can be composed into larger structures.

A structure to hold external entries.

This is used to associate textual names with addresses.

A list of cod_extern_entry structures. This is used to specify the addresses of external functions or variables that a function may access.

typedef struct _cod_code_struct * cod_code

cod_code is a handle to the generated code. In addition to the generated code block, it holds external data pointers and supplemental information.

typedef void(* err_out_func_t)(void *client_data, char *string)

err_out_func_t is a function pointer type. Functions matching this profile can be used as call-out handlers for COD errors.

Parameters:
client_dataan uninspected value passed in from cod_set_error_func()
stringthe textual representation of the error.

Function Documentation

Create a handle to a new cod_parse_context.

This is generally the first call in dynamically generating a subroutine/function. The cod_parse_context created here is used in all future calls, setting up the parameter profile and generating the code.

Returns:
Will return a new initialized cod_parse_context unless there is no available memory.

Free a handle to an cod_parse_context.

Parameters:
contextthe cod_parse_context to be free'd. Calling this routine frees all memory associated with the parse context, but not that of code that has been generated from this context.
void cod_assoc_externs ( cod_parse_context  context,
cod_extern_list  externs 
)

Associate a set of "name, external address" pairs with a parse context

These names and addresses are used to resolve (external references in generated code). In addition to associating the addresses, the full prototypes of the functions or variables must be added to the parse context, usually with cod_parse_for_context().

Parameters:
contextthe cod_parse_context to which the externs are associated.
externsthe list of "name, external address" pairs to be associated. This list should be terminated with a {NULL, 0} pair.
void cod_subroutine_declaration ( const char *  decl,
cod_parse_context  context 
)

This is used to establish the parameter profile and return type of the subroutine to be generated.

Parameters:
declthe textual declaration for the subroutine
contextthe context for subroutine generation

The 'decl' parameter is a C-style declaration, without a terminating semicolon. I.E. something like: "int proc(double d, int *i)"

void cod_add_simple_struct_type ( const char *  name,
FMFieldList  field_list,
cod_parse_context  context 
)

Make a structured type, represented by a FFS-style FMFieldList, available in a particular context.

Parameters:
namethe name to be associated with the new structured type.
field_listthe FFS-style FMFieldList that describes the layout of the structure.
contextthe context in which the type is to be made available.
void cod_add_struct_type ( FMStructDescList  format_list,
cod_parse_context  context 
)

Make a set of structured types, represented by a FFS-style FMStructDescList, available in a particular context.

Parameters:
format_listthe FFS-style FMStructDescList that describes the layout of the structures.
contextthe context in which the type is to be made available.
void cod_add_param ( const char *  id,
const char *  typ,
int  param_num,
cod_parse_context  context 
)

As an alternative to cod_subroutine_declaration(), add declaration for the Nth parameter.

Parameters:
namethe name to be associated with the new parameter.
typthe data type of the new parameter.
param_numthe numeral of the new parameter (0 is first)
contextthe context in which the subroutine is being declared.
void cod_set_return_type ( char *  typ,
cod_parse_context  context 
)

add a declaration for the Nth parameter as an FFS-encoded structure

Parameters:
namethe name to be associated with the new parameter.
dataan encoded data block
param_numthe numeral of the new parameter (0 is first)
can FMContext value in which to format information for the data is available
contextthe context in which the subroutine is being declared.

As an alternative to cod_subroutine_declaration(), set the return type of a subroutine

Parameters:
typthe data type of the return value.
contextthe context in which the subroutine is being declared.
cod_code cod_code_gen ( char *  code,
cod_parse_context  context 
)

generate code for a function body in a given context

Parameters:
codethe string representing the function body.
contextthe context in which the function body is to be generated.
int cod_code_verify ( char *  code,
cod_parse_context  context 
)

perform syntactical and semantic checking of a function body without actually generating code.

Parameters:
codethe string representing the function body.
contextthe context in which the function body is to be checked.
void cod_code_free ( cod_code  code)

Free all resources associated with the generated code associated with the cod_code handle.

Parameters:
codethe handle to the resources that will be free'd.

create an execution context associated with a code block

Parameters:
codethe handle to the code bloc
Returns:
the created execution context

Free all resources associated with the generated code associated with the cod_exec_context handle.

Parameters:
codethe handle to the resources that will be free'd.
void cod_assoc_client_data ( cod_exec_context  ec,
int  key,
long  value 
)

Associate application-level data with an execution context. This is typically done so that it can be retrieved by a called subprocess. The key value is any 32-bit value, presumably chosen to minimize the possibility of conflict. The value that can be associated is a long.

Parameters:
ecthe cod_exec_context value to which to associate the data
keythe value that will serve as a key to retrieve the data
valuethe 'long' data that will be associated with the key
long cod_get_client_data ( cod_exec_context  ec,
int  key 
)

Retrieve application-level data with an execution context. This is typically done by a called subprocess to retrieve data that was available to the broader application. The key value is any 32-bit value, presumably chosen to minimize the possibility of conflict. The value returned is one which had been associated via cod_assoc_client_data(). If no value had been associated, -1 is returned. *

Parameters:
ecthe cod_exec_context value to which to associate the data
keythe value that will serve as a key to retrieve the data
Returns:
the 'long' data that was associated with the key
void* cod_extract_state ( cod_exec_context  ec,
int *  length_p 
)

Extract static state from an execution context.

Parameters:
ecthe cod_exec_context value from which to extract static state
length_pointera pointer to an integer which will be set to the length of the returned state block
Returns:
a pointer to the extracted state
int cod_install_state ( cod_exec_context  ec,
void *  state,
int  length 
)

Install static state into an execution context.

Parameters:
ecthe cod_exec_context value into which to install static state
statethe state block to install
state_sizethe size of the state block
Returns:
1 on success, 0 on failure
int cod_parse_for_context ( char *  code,
cod_parse_context  context 
)

This parses a string to determine what external functions and variables are visible during code generation.

The input string is of standard C syntax for external variable and function declarations. All previously registered types are available.

Parameters:
codeThe input string containing declarations.
contextThe parse context in which the declarations should be visible.
int cod_parse_for_globals ( char *  code,
cod_parse_context  context 
)

This parses a string to setup global variables that are visible during code generation.

The input string is of standard C syntax for external variable and function declarations. All previously registered types are available.

Parameters:
codeThe input string containing declarations.
contextThe parse context in which the declarations should be visible.

Duplicate a handle to an cod_parse_context.

Parameters:
contextthe cod_parse_context to be duplicated.

Duplicate a handle to an cod_parse_context, specifically adapting the results to reuse the global declarations of the prior context. This is a specialized function that helps run external regression tests and may not be broadly useful.

Parameters:
contextthe cod_parse_context to be duplicated.
void cod_set_error_func ( cod_parse_context  context,
err_out_func_t  err_func 
)

cod_set_error_func establishes a new error output routine for COD.

By default, errors are sent to stdout. If the error output is set then errors can be captured for other disposition.

Parameters:
contextthe context in which errors are to be captured
err_functhe function to be called when errors occur
void cod_set_dont_coerce_return ( cod_parse_context  context,
int  value 
)

cod_set_dont_coerce_return restricts COD to more strict type matching for expressions and return values

Parameters:
contextthe context to restrict
valueTrue if coercion is not to be applied to return values, false by default
void cod_dump ( cod_code  code)

This will dump (to stdout) a disassembled version of the machine code that has been generated

Parameters:
codethe cod_code handle containing the code to be dumped.
cod_code gen_rollback_code ( FMStructDescList  format1,
FMStructDescList  format2,
char *  xform_code 
)

This will generate rollback code for message morphing

Parameters:
format1the new format. This is the format of the input message.
format2the old format. This is the format of the output message.
xform_codeThe COD code string that transforms data from format1 to format2.
void cod_add_int_constant_to_parse_context ( const char *  id,
int  value,
cod_parse_context  context 
)

This is used to add an integer constant available in a particular context.

Parameters:
idThe name to be associated with the type.
valueThe value of the constant
contextthe context in which this is to be created
void cod_set_closure ( char *  name,
void *  value,
cod_parse_context  context 
)

Sets what value cod_closure_context will send to a subroutine.

Parameters:
nameThe name of the (external) subroutine.
valueThe value to send
contextThe context in which the subroutine has been declared.