The Code API now supports labels

This commit is contained in:
Pierre Pronchery 2008-06-12 05:40:19 +00:00
parent 5930c622d1
commit bf02939f40
2 changed files with 54 additions and 15 deletions

View File

@ -29,9 +29,11 @@
#include "../config.h" #include "../config.h"
#ifdef DEBUG #ifdef DEBUG
# define DEBUG_FUNC() fprintf(stderr, "DEBUG: %s()\n", __func__);
# define DEBUG_SCOPE() fprintf(stderr, "DEBUG: %s() %zu\n", __func__, \ # define DEBUG_SCOPE() fprintf(stderr, "DEBUG: %s() %zu\n", __func__, \
code->scopes_cnt); code->scopes_cnt);
#else #else
# define DEBUG_FUNC()
# define DEBUG_SCOPE() # define DEBUG_SCOPE()
#endif #endif
@ -70,6 +72,7 @@ typedef struct _CodeIdentifier
{ {
CodeContext context; CodeContext context;
CodeStorage storage; CodeStorage storage;
/* XXX consider copying the original token instead */
char * name; char * name;
} CodeIdentifier; } CodeIdentifier;
@ -85,6 +88,7 @@ static int _code_target_exit(Code * code);
static int _code_target_function_begin(Code * code, char const * name); static int _code_target_function_begin(Code * code, char const * name);
static int _code_target_function_call(Code * code, char const * name); static int _code_target_function_call(Code * code, char const * name);
static int _code_target_function_end(Code * code); static int _code_target_function_end(Code * code);
static int _code_target_label_set(Code * code, char const * name);
/* protected */ /* protected */
@ -149,9 +153,7 @@ static int _code_context_queue_identifier(Code * code, char const * identifier)
/* code_target_init */ /* code_target_init */
static int _code_target_init(Code * code, char const * outfile, int optlevel) static int _code_target_init(Code * code, char const * outfile, int optlevel)
{ {
#ifdef DEBUG DEBUG_FUNC();
fprintf(stderr, "DEBUG: %s()\n", __func__);
#endif
if(code->target->init == NULL) if(code->target->init == NULL)
return 0; return 0;
return code->target->init(outfile, optlevel); return code->target->init(outfile, optlevel);
@ -161,9 +163,7 @@ static int _code_target_init(Code * code, char const * outfile, int optlevel)
/* code_target_exit */ /* code_target_exit */
static int _code_target_exit(Code * code) static int _code_target_exit(Code * code)
{ {
#ifdef DEBUG DEBUG_FUNC();
fprintf(stderr, "DEBUG: %s()\n", __func__);
#endif
if(code->target->exit == NULL) if(code->target->exit == NULL)
return 0; return 0;
return code->target->exit(); return code->target->exit();
@ -197,15 +197,25 @@ static int _code_target_function_call(Code * code, char const * name)
/* code_target_function_end */ /* code_target_function_end */
static int _code_target_function_end(Code * code) static int _code_target_function_end(Code * code)
{ {
#ifdef DEBUG DEBUG_FUNC();
fprintf(stderr, "DEBUG: %s()\n", __func__);
#endif
if(code->target->function_end == NULL) if(code->target->function_end == NULL)
return 0; return 0;
return code->target->function_end(); return code->target->function_end();
} }
/* code_target_label_set */
static int _code_target_label_set(Code * code, char const * name)
{
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s(\"%s\")\n", __func__, name);
#endif
if(code->target->label_set == NULL)
return 0;
return code->target->label_set(name);
}
/* public */ /* public */
/* functions */ /* functions */
/* code_new */ /* code_new */
@ -286,9 +296,7 @@ int code_delete(Code * code)
CodeScope * scope; CodeScope * scope;
size_t i; size_t i;
#ifdef DEBUG DEBUG_FUNC();
fprintf(stderr, "DEBUG: %s()\n", __func__);
#endif
if(code->plugin != NULL) if(code->plugin != NULL)
{ {
if(code->target != NULL) if(code->target != NULL)
@ -327,6 +335,7 @@ CodeContext code_context_get(Code * code)
/* code_context_set */ /* code_context_set */
int code_context_set(Code * code, CodeContext context) int code_context_set(Code * code, CodeContext context)
/* XXX use assertions? */
{ {
int ret = 0; int ret = 0;
size_t i; size_t i;
@ -379,6 +388,14 @@ int code_context_set(Code * code, CodeContext context)
} }
_code_context_flush(code); _code_context_flush(code);
break; break;
case CODE_CONTEXT_LABEL:
if(code->identifiers_cnt == 1)
{
code->context = CODE_CONTEXT_LABEL;
ret |= code_context_set_identifier(code,
code->identifiers[0].name);
}
_code_context_flush(code);
default: default:
break; break;
} }
@ -431,6 +448,8 @@ int code_context_set_identifier(Code * code, char const * identifier)
return code_function_begin(code, identifier); return code_function_begin(code, identifier);
case CODE_CONTEXT_FUNCTION_CALL: case CODE_CONTEXT_FUNCTION_CALL:
return code_function_call(code, identifier); return code_function_call(code, identifier);
case CODE_CONTEXT_LABEL:
return code_label_set(code, identifier);
case CODE_CONTEXT_PARAMETERS: case CODE_CONTEXT_PARAMETERS:
return _code_context_queue_identifier(code, identifier); return _code_context_queue_identifier(code, identifier);
case CODE_CONTEXT_PARAMETERS_TYPE: case CODE_CONTEXT_PARAMETERS_TYPE:
@ -451,7 +470,11 @@ int code_context_set_storage(Code * code, CodeStorage storage)
char const * str[CODE_STORAGE_COUNT] = char const * str[CODE_STORAGE_COUNT] =
{ {
"NULL", "NULL",
"TYPEDEF" "TYPEDEF",
"EXTERN",
"STATIC",
"AUTO",
"REGISTER"
}; };
fprintf(stderr, "DEBUG: %s(%s)\n", __func__, str[storage]); fprintf(stderr, "DEBUG: %s(%s)\n", __func__, str[storage]);
@ -494,6 +517,13 @@ int code_function_end(Code * code)
} }
/* code_label_set */
int code_label_set(Code * code, char const * name)
{
return _code_target_label_set(code, name);
}
/* scope */ /* scope */
/* code_scope_push /* code_scope_push
* PRE * PRE
@ -547,6 +577,7 @@ int code_scope_pop(Code * code)
/* types */ /* types */
/* code_type_add */ /* code_type_add */
int code_type_add(Code * code, char const * name) int code_type_add(Code * code, char const * name)
/* FIXME consider using a Token instead of the name */
{ {
size_t i; size_t i;
CodeType * p; CodeType * p;

View File

@ -45,6 +45,7 @@ typedef enum _CodeContext
CODE_CONTEXT_FUNCTION_END, CODE_CONTEXT_FUNCTION_END,
CODE_CONTEXT_FUNCTION_PARAMETERS, CODE_CONTEXT_FUNCTION_PARAMETERS,
CODE_CONTEXT_FUNCTION_START, CODE_CONTEXT_FUNCTION_START,
CODE_CONTEXT_LABEL,
CODE_CONTEXT_PARAMETERS, CODE_CONTEXT_PARAMETERS,
CODE_CONTEXT_PARAMETERS_TYPE, CODE_CONTEXT_PARAMETERS_TYPE,
CODE_CONTEXT_PRIMARY_EXPR, CODE_CONTEXT_PRIMARY_EXPR,
@ -57,9 +58,13 @@ typedef enum _CodeContext
typedef enum _CodeStorage typedef enum _CodeStorage
{ {
CODE_STORAGE_NULL = 0x0, CODE_STORAGE_NULL = 0x0,
CODE_STORAGE_TYPEDEF = 0x1 CODE_STORAGE_TYPEDEF = 0x1,
CODE_STORAGE_EXTERN = 0x2,
CODE_STORAGE_STATIC = 0x4,
CODE_STORAGE_AUTO = 0x8,
CODE_STORAGE_REGISTER = 0x10
} CodeStorage; } CodeStorage;
# define CODE_STORAGE_LAST CODE_STORAGE_TYPEDEF # define CODE_STORAGE_LAST CODE_STORAGE_REGISTER
# define CODE_STORAGE_COUNT (CODE_STORAGE_LAST + 1) # define CODE_STORAGE_COUNT (CODE_STORAGE_LAST + 1)
@ -79,6 +84,9 @@ int code_function_begin(Code * code, char const * name);
int code_function_call(Code * code, char const * name); int code_function_call(Code * code, char const * name);
int code_function_end(Code * code); int code_function_end(Code * code);
/* labels */
int code_label_set(Code * code, char const * name);
/* scope */ /* scope */
int code_scope_push(Code * code); int code_scope_push(Code * code);
int code_scope_pop(Code * code); int code_scope_pop(Code * code);