Begin working on flow analysis.

For now, variable and flow graph node maps are built.
This commit is contained in:
Bill Currie 2012-10-30 17:41:40 +09:00
parent f30010b367
commit 739ebc522c
9 changed files with 229 additions and 12 deletions

View file

@ -1,7 +1,7 @@
AUTOMAKE_OPTIONS= foreign
EXTRA_DIST= class.h codespace.h cpp.h dags.h debug.h def.h defspace.h \
diagnostic.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 set.h shared.h statements.h strpool.h struct.h switch.h \
symtab.h type.h value.h
diagnostic.h emit.h expr.h flow.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 set.h shared.h statements.h strpool.h struct.h \
switch.h symtab.h type.h value.h

View file

@ -45,6 +45,7 @@ typedef struct daglabel_s {
const char *opcode; ///< not if op
struct operand_s *op; ///< not if opcode;
struct dagnode_s *dagnode; ///< node with which this label is associated
int number; ///< number of variable in func's ref list
} daglabel_t;
typedef struct dagnode_s {
@ -73,6 +74,8 @@ struct dstring_s;
void print_dag (struct dstring_s *dstr, dagnode_t *node);
struct sblock_s;
daglabel_t *operand_label (struct operand_s *op);
/** Make a dag for a single basic block.
\param block The basic block for which the dag will be created.

45
tools/qfcc/include/flow.h Normal file
View file

@ -0,0 +1,45 @@
/*
flow.h
Flow graph analysis.
Copyright (C) 2012 Bill Currie <bill@taniwha.org>
Author: Bill Currie <bill@taniwha.org>
Date: 2012/10/30
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifndef flow_h
#define flow_h
/** \defgroup qfcc_flow Flow graph analysis
\ingroup qfcc
*/
//@{
struct function_s;
void flow_build_vars (struct function_s *func);
void flow_build_graph (struct function_s *func);
//@}
#endif//flow_h

View file

@ -79,6 +79,21 @@ typedef struct function_s {
struct reloc_s *refs; ///< relocation targets for this function
struct expr_s *var_init;
const char *name; ///< nice name for __PRETTY_FUNCTION__
struct sblock_s *sblock; ///< initial node of function's flow graph
/** Array of pointers to all nodes in the function's flow graph.
This permits ready mapping of node number to node in the flow
analyzer.
*/
struct sblock_s **graph;
int num_nodes; ///< number of nodes in the graph
/** Array of pointers to all variables referenced by the function's code.
This permits ready mapping of (function specific) variable number to
variable in the flow analyzer.
*/
struct daglabel_s **vars;
int num_vars; ///< total number of variables referenced
} function_t;
extern function_t *current_func;

View file

@ -76,6 +76,7 @@ typedef struct sblock_s {
struct dagnode_s *dag;
int offset; ///< offset of first statement of block
int reachable;
int number; ///< number of this block in flow graph
statement_t *statements;
statement_t **tail;
} sblock_t;

View file

@ -41,9 +41,9 @@ bin_SCRIPTS= qfpreqcc
common_src=\
class.c codespace.c constfold.c cpp.c dags.c debug.c def.c defspace.c \
diagnostic.c dot_dag.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 set.c shared.c statements.c strpool.c \
struct.c switch.c symtab.c type.c value.c
flow.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 set.c shared.c statements.c \
strpool.c struct.c switch.c symtab.c type.c value.c
qfcc_SOURCES= qc-lex.l qc-parse.y qp-lex.l qp-parse.y $(common_src)
qfcc_LDADD= $(QFCC_LIBS)

View file

@ -119,7 +119,7 @@ opcode_label (const char *opcode)
return label;
}
static daglabel_t *
daglabel_t *
operand_label (operand_t *op)
{
operand_t *o;

151
tools/qfcc/source/flow.c Normal file
View file

@ -0,0 +1,151 @@
/*
flow.c
Flow graph analysis
Copyright (C) 2012 Bill Currie <bill@taniwha.org>
Author: Bill Currie <bill@taniwha.org>
Date: 2012/10/30
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdlib.h>
#include "QF/dstring.h"
#include "dags.h"
#include "flow.h"
#include "function.h"
#include "statements.h"
#include "symtab.h"
static int
is_variable (daglabel_t *var)
{
operand_t *o;
if (!var)
return 0;
o = var->op;
while (o->op_type == op_alias)
o = o->o.alias;
if (o->op_type == op_temp)
return 1;
if (o->op_type != op_symbol)
return 0;
if (o->o.symbol->sy_type == sy_var)
return 1;
//FIXME functions? (some are variable)
return 0;
}
static int
count_operand (operand_t *op)
{
daglabel_t *var;
if (!op)
return 0;
if (op->op_type == op_label)
return 0;
var = operand_label (op);
// daglabels are initialized with number == 0, and any global daglabel
// used by a function will always have a number >= 0 after flow analysis,
// and local daglabels will always be 0 before flow analysis, so use -1
// to indicate the variable has been counted.
if (is_variable (var) && var->number != -1) {
var->number = -1;
return 1;
}
return 0;
}
static void
add_operand (function_t *func, operand_t *op)
{
daglabel_t *var;
if (!op)
return;
if (op->op_type == op_label)
return;
var = operand_label (op);
// If the daglabel number is still -1, then the daglabel has not yet been
// added to the list of variables referenced by the function.
if (is_variable (var) && var->number == -1) {
var->number = func->num_vars++;
func->vars[var->number] = var;
}
}
void
flow_build_vars (function_t *func)
{
sblock_t *sblock;
statement_t *s;
int num_vars;
for (num_vars = 0, sblock = func->sblock; sblock; sblock = sblock->next) {
for (s = sblock->statements; s; s = s->next) {
num_vars += count_operand (s->opa);
num_vars += count_operand (s->opb);
num_vars += count_operand (s->opc);
}
}
func->vars = malloc (num_vars * sizeof (daglabel_t *));
func->num_vars = 0; // incremented by add_operand
for (sblock = func->sblock; sblock; sblock = sblock->next) {
for (s = sblock->statements; s; s = s->next) {
add_operand (func, s->opa);
add_operand (func, s->opb);
add_operand (func, s->opc);
}
}
}
void
flow_build_graph (function_t *func)
{
sblock_t *sblock;
int num_blocks = 0;
for (sblock = func->sblock; sblock; sblock = sblock->next)
sblock->number = num_blocks++;
func->graph = malloc (num_blocks * sizeof (sblock_t *));
for (sblock = func->sblock; sblock; sblock = sblock->next)
func->graph[sblock->number] = sblock;
func->num_nodes = num_blocks;
}

View file

@ -52,6 +52,7 @@
#include "diagnostic.h"
#include "emit.h"
#include "expr.h"
#include "flow.h"
#include "function.h"
#include "opcodes.h"
#include "options.h"
@ -639,12 +640,13 @@ finish_function (function_t *f)
void
emit_function (function_t *f, expr_t *e)
{
sblock_t *sblock;
f->code = pr.code->size;
lineno_base = f->def->line;
sblock = make_statements (e);
emit_statements (sblock);
f->sblock = make_statements (e);
flow_build_graph (f);
flow_build_vars (f);
printf ("%s %d %d\n", f->name, f->num_nodes, f->num_vars);
emit_statements (f->sblock);
}
int