Fix constant value type handling.

Rename immediate.[ch] to value.[ch] and clean up the mess.

Add convert_value() to convert the type of a value (only scalar types).
This commit is contained in:
Bill Currie 2011-03-22 12:24:39 +09:00
parent b812c0e235
commit f1d9570dc8
20 changed files with 79 additions and 31 deletions

View file

@ -1,6 +1,6 @@
AUTOMAKE_OPTIONS= foreign
EXTRA_DIST= class.h codespace.h cpp.h debug.h def.h defspace.h diagnostic.h \
emit.h expr.h function.h grab.h idstuff.h immediate.h linker.h \
method.h obj_file.h obj_type.h opcodes.h options.h qfcc.h qfprogs.h \
reloc.h statements.h strpool.h struct.h switch.h symtab.h type.h
emit.h expr.h function.h grab.h idstuff.h linker.h method.h \
obj_file.h obj_type.h opcodes.h options.h qfcc.h qfprogs.h reloc.h \
statements.h strpool.h struct.h switch.h symtab.h type.h value.h

View file

@ -1,7 +1,7 @@
/*
immediate.h
value.h
shared immediate value handling
value handling
Copyright (C) 2002 Bill Currie <bill@taniwha.org>
@ -29,14 +29,17 @@
$Id$
*/
#ifndef __immediate_h
#define __immediate_h
#ifndef __value_h
#define __value_h
struct ex_value_s;
struct type_s;
void convert_value (struct ex_value_s *value, struct type_s *type);
struct def_s *emit_value (struct ex_value_s *value, struct def_s *def);
int ReuseString (const char *str);
void clear_immediates (void);
#endif//__immediate_h
#endif//__value_h

View file

@ -53,10 +53,10 @@ EXTRA_PROGRAMS= qfcc qfpc qfprogs
common_src=\
class.c codespace.c constfold.c cpp.c debug.c def.c defspace.c \
diagnostic.c dot_expr.c dot_flow.c emit.c \
expr.c function.c grab.c idstuff.c immediate.c linker.c method.c \
obj_file.c obj_type.c opcodes.c options.c qfcc.c reloc.c statements.c \
strpool.c struct.c switch.c symtab.c type.c
diagnostic.c dot_expr.c dot_flow.c emit.c expr.c function.c grab.c \
idstuff.c linker.c method.c obj_file.c obj_type.c opcodes.c options.c \
qfcc.c reloc.c statements.c strpool.c struct.c switch.c symtab.c type.c \
value.c
qfcc_SOURCES= qc-lex.l qc-parse.y $(common_src)
qfcc_LDADD= $(QFCC_LIBS)

View file

@ -56,7 +56,6 @@ static __attribute__ ((used)) const char rcsid[] =
#include "diagnostic.h"
#include "emit.h"
#include "expr.h"
#include "immediate.h"
#include "method.h"
#include "options.h"
#include "reloc.h"
@ -64,6 +63,7 @@ static __attribute__ ((used)) const char rcsid[] =
#include "struct.h"
#include "symtab.h"
#include "type.h"
#include "value.h"
static hashtab_t *class_hash;
static hashtab_t *category_hash;

View file

@ -48,9 +48,9 @@ static __attribute__ ((used)) const char rcsid[] =
#include "debug.h"
#include "diagnostic.h"
#include "expr.h"
#include "immediate.h"
#include "qfcc.h"
#include "strpool.h"
#include "value.h"
int lineno_base;

View file

@ -53,13 +53,13 @@ static __attribute__ ((used)) const char rcsid[] =
#include "diagnostic.h"
#include "emit.h"
#include "expr.h"
#include "immediate.h"
#include "options.h"
#include "reloc.h"
#include "strpool.h"
#include "struct.h"
#include "symtab.h"
#include "type.h"
#include "value.h"
static def_t *free_defs;
@ -414,6 +414,7 @@ initialize_def (symbol_t *sym, type_t *type, expr_t *init, defspace_t *space,
error (0, "non-constant initializier");
} else {
sym->s.value = init->e.value;
convert_value (&sym->s.value, sym->type);
}
return;
}

View file

@ -49,7 +49,6 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$";
#include "defspace.h"
#include "diagnostic.h"
#include "expr.h"
#include "immediate.h"
#include "options.h"
#include "reloc.h"
#include "strpool.h"

View file

@ -51,7 +51,6 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$";
#include "diagnostic.h"
#include "emit.h"
#include "function.h"
#include "immediate.h"
#include "opcodes.h"
#include "options.h"
#include "qfcc.h"
@ -59,6 +58,7 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$";
#include "statements.h"
#include "symtab.h"
#include "type.h"
#include "value.h"
static def_t zero_def;

View file

@ -56,7 +56,6 @@ static __attribute__ ((used)) const char rcsid[] =
#include "expr.h"
#include "function.h"
#include "idstuff.h"
#include "immediate.h"
#include "method.h"
#include "options.h"
#include "reloc.h"

View file

@ -56,7 +56,6 @@ static __attribute__ ((used)) const char rcsid[] =
#include "emit.h"
#include "expr.h"
#include "function.h"
#include "immediate.h"
#include "opcodes.h"
#include "options.h"
#include "reloc.h"
@ -64,6 +63,7 @@ static __attribute__ ((used)) const char rcsid[] =
#include "strpool.h"
#include "symtab.h"
#include "type.h"
#include "value.h"
static param_t *free_params;
static function_t *free_functions;

View file

@ -64,7 +64,6 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$";
#include "diagnostic.h"
#include "emit.h"
#include "expr.h"
#include "immediate.h"
#include "linker.h"
#include "obj_file.h"
#include "obj_type.h"

View file

@ -55,7 +55,6 @@ static __attribute__ ((used)) const char rcsid[] =
#include "defspace.h"
#include "diagnostic.h"
#include "emit.h"
#include "immediate.h"
#include "method.h"
#include "options.h"
#include "reloc.h"
@ -63,6 +62,7 @@ static __attribute__ ((used)) const char rcsid[] =
#include "struct.h"
#include "symtab.h"
#include "type.h"
#include "value.h"
static hashtab_t *known_methods;

View file

@ -53,7 +53,6 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$";
#include "emit.h"
#include "expr.h"
#include "function.h"
#include "immediate.h"
#include "obj_file.h"
#include "obj_type.h"
#include "options.h"
@ -63,6 +62,7 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$";
#include "strpool.h"
#include "symtab.h"
#include "type.h"
#include "value.h"
static int
count_relocs (reloc_t *r)

View file

@ -49,11 +49,11 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$";
#include "defspace.h"
#include "diagnostic.h"
#include "emit.h"
#include "immediate.h"
#include "obj_type.h"
#include "qfcc.h"
#include "reloc.h"
#include "symtab.h"
#include "value.h"
#define ENC_DEF(dest,def) EMIT_DEF (pr.type_data, dest, def)
#define ENC_STR(dest,str) \

View file

@ -53,13 +53,13 @@ static __attribute__ ((used)) const char rcsid[] =
#include "diagnostic.h"
#include "expr.h"
#include "grab.h"
#include "immediate.h"
#include "options.h"
#include "qfcc.h"
#include "strpool.h"
#include "struct.h"
#include "symtab.h"
#include "type.h"
#include "value.h"
#include "qc-parse.h"

View file

@ -53,7 +53,6 @@ static __attribute__ ((used)) const char rcsid[] =
#include "emit.h"
#include "expr.h"
#include "function.h"
#include "immediate.h"
#include "method.h"
#include "options.h"
#include "qfcc.h"
@ -63,6 +62,7 @@ static __attribute__ ((used)) const char rcsid[] =
#include "switch.h"
#include "symtab.h"
#include "type.h"
#include "value.h"
#define YYDEBUG 1
#define YYERROR_VERBOSE 1

View file

@ -78,7 +78,6 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$";
#include "expr.h"
#include "function.h"
#include "idstuff.h"
#include "immediate.h"
#include "linker.h"
#include "method.h"
#include "obj_file.h"
@ -89,6 +88,7 @@ static __attribute__ ((used)) const char rcsid[] = "$Id$";
#include "struct.h"
#include "symtab.h"
#include "type.h"
#include "value.h"
options_t options;

View file

@ -55,7 +55,6 @@ static __attribute__ ((used)) const char rcsid[] =
#include "diagnostic.h"
#include "emit.h"
#include "expr.h"
#include "immediate.h"
#include "obj_type.h"
#include "qfcc.h"
#include "reloc.h"

View file

@ -9,12 +9,12 @@
#include "defspace.h"
#include "emit.h"
#include "function.h"
#include "immediate.h"
#include "obj_file.h"
#include "options.h"
#include "qfcc.h"
#include "strpool.h"
#include "type.h"
#include "value.h"
struct dstring_s;
options_t options;

View file

@ -1,7 +1,7 @@
/*
immediate.c
value.c
shared immediate value handling
value handling
Copyright (C) 2002 Bill Currie <bill@taniwha.org>
@ -50,13 +50,14 @@ static __attribute__ ((used)) const char rcsid[] =
#include "qfcc.h"
#include "def.h"
#include "defspace.h"
#include "diagnostic.h"
#include "emit.h"
#include "expr.h"
#include "immediate.h"
#include "reloc.h"
#include "strpool.h"
#include "symtab.h"
#include "type.h"
#include "value.h"
typedef struct {
def_t *def;
@ -156,6 +157,53 @@ ReuseString (const char *str)
return strpool_addstr (pr.strings, str);
}
static float
value_as_float (ex_value_t *value)
{
if (value->type == ev_integer)
return value->v.integer_val;
if (value->type == ev_short)
return value->v.short_val;
if (value->type == ev_float)
return value->v.float_val;
return 0;
}
static float
value_as_int (ex_value_t *value)
{
if (value->type == ev_integer)
return value->v.integer_val;
if (value->type == ev_short)
return value->v.short_val;
if (value->type == ev_float)
return value->v.float_val;
return 0;
}
void
convert_value (ex_value_t *value, type_t *type)
{
if (!is_scalar (type) || !is_scalar (ev_types[value->type])) {
error (0, "unable to convert non-scalar value");
return;
}
if (is_float (type)) {
float val = value_as_float (value);
value->type = ev_float;
value->v.float_val = val;
} else if (type->type == ev_short) {
int val = value_as_int (value);
value->type = ev_short;
value->v.short_val = val;
} else {
//FIXME handle enums separately?
int val = value_as_int (value);
value->type = ev_integer;
value->v.integer_val = val;
}
}
def_t *
emit_value (ex_value_t *value, def_t *def)
{