Commit Graph

65 Commits

Author SHA1 Message Date
Bill Currie 75be251d65 Create and use def_visit_all.
Even though it turned out there were only two copies of the def alias
handling code, I got tired of writing it. The code is easier to read, too
:)
2012-12-12 12:01:31 +09:00
Bill Currie 76c9aa2930 Handle alias defs when building a dag.
When an alais def (or aliased def) is used, any overlapping aliases that
have previously been assigned need to be marked as live, and edges to the
aliases added to the new node. However, when assigned to, live-forcing
needs to be turned off.

This fixes the lost assignments to .super.
2012-12-11 19:57:19 +09:00
Bill Currie 7607c7d649 Resurrect alias operands.
It turns out they are necessary for the code output from dags. This fixes
the ice for *to = *from++;
2012-12-11 15:52:37 +09:00
Bill Currie 8452f69a21 Use only the real temp for daglabels.
This fixes the bogus temps for "*to = *from++;", but qfcc ices due to the
operand types being lost. It seems alias operands need to be resurrected,
if only for code output by dags.
2012-12-11 15:20:52 +09:00
Bill Currie e0c92b6089 Rename set_iter_t's member to value.
Makes more sense now that the membership of the value depends on the
inversion of the set.
2012-12-06 21:11:38 +09:00
Bill Currie eb8fd55677 Move set.c into libQFutil.
Also move the ALLOC/FREE macros from qfcc.h to QF/alloc.h (needed to for
set.c).

Both modules are more generally useful than just for qfcc (eg, set
builtins for ruamoko).
2012-12-06 20:52:53 +09:00
Bill Currie 3f3b501c58 Move flowvar/deflabel from symbol_t to def_t.
With the need to handle aliasing in the optimizer, it has become apparent
that having the flow data attached to symbols is not nearly as useful as
having it attached to defs (which are views of the actual variables).

This also involves a bit of a cleanup of operand types: op_pointer and
op_alias are gone (this seems to greatly simplify the optimizer)

There is a bit of a problem with enums in switch statements, but this might
actually be a sign that something is not quite right in the switch code
(other than enums not being recognized as ints for jump table
optimization).
2012-12-05 19:47:22 +09:00
Bill Currie 5e9d7d3567 Don't use the expr type for assignments.
Instead, the type is taken from the source operand (this means the computed
type of .return for that var). ctf now builds :)
2012-11-30 21:06:08 +09:00
Bill Currie 738fc65e0f Treat global vars as always live.
This fixes the infloop.r test.
2012-11-24 14:56:30 +09:00
Bill Currie bdafdad0d5 Treat flow control statements separately.
I'm not convinced this is the correct fix for while (count--), but it does
work.
2012-11-24 12:53:51 +09:00
Bill Currie 607d7dd45e Simplify the child match in dagnode_match.
I have no idea why I did it that way when all that's needed is to check the
child pointers. Well, that's modulo.r fixed :)
2012-11-22 21:59:01 +09:00
Bill Currie 4b5e00afdf Do a bunch of refactoring.
The dag creation loop was getting too messy.
2012-11-21 18:30:44 +09:00
Bill Currie 2c09464e80 Don't attach a dag label to its own node.
This kills a = a; statements :)
2012-11-21 12:16:10 +09:00
Bill Currie 87121b4cf1 Be slightly picky about which nodes to kill.
Constant/label nodes should never be killed because they can (in theory)
never change. While constants /can/ change in the Quake VM, it's not worth
worrying about as there would be much more important things to worry about
(like 2+2 not giving 4).

Due to the hoops one would have to jump through, it is assumed that a
pointer or an offset from that pointer will never overwrite the pointer.
2012-11-20 15:28:57 +09:00
Bill Currie 22d72f33d2 Match pointer dereferences with pointer assignements.
Having the source operand of a pointer assignment available to later
instrctions can make for more efficient code as the value does not need to
be dereferenced later. For this purpose, pointer dereference dag nodes now
store the source operand as their value, and dagnode_match will match x=a.b
with *(a+b)=y so long as both a and b are the same in both nodes. x and y
are irrelevant to the match. The resulting code will be the equivalent of:
    *(a+b) = y;
    x = y;
2012-11-20 15:22:05 +09:00
Bill Currie 36e359e2ee Correct a typo. 2012-11-20 15:19:32 +09:00
Bill Currie ef0f30935f Kill all dag nodes on pointer assignment.
It's brutal, but it works and is conservatively correct.
2012-11-20 14:30:42 +09:00
Bill Currie f83cf1748f Allow node() to work for all operand types.
This is a bit of an expermiment to get better dags (more node reuse).
2012-11-20 14:23:43 +09:00
Bill Currie 1472fec7f2 Factor out the live vars magic from dag_create.
The code is messy enough as it is :P
2012-11-20 13:44:36 +09:00
Bill Currie 56103f9a38 Treat global variables as live at the end of a function.
.return and .param_N are not classed as global variables for data flow
analysis. .return is taken care of by return statements, and .param_N by
call statements.

With this, the menus work up to attempting to load the menu plist.
Something is corrupting zmalloc's blocks.
2012-11-19 17:21:35 +09:00
Bill Currie bcde7396a0 Don't let generate_assignments guess the type.
It fails when both source and destination are void, but since the dag node
itself does know the correct type, pass in the type rather than guessing.
2012-11-19 15:21:01 +09:00
Bill Currie 68bf013601 Use a dag-local live vars set.
Function calls need to ensure .param_N actually get assigned, and so the
params must be seen as live by the dead variable removal code. However, it
is undesirable to modify the live vars data of the flow node, so make a
local copy.
2012-11-19 15:18:31 +09:00
Bill Currie d1252813ce Use low_level_type instead of extract_type.
extract_type doesn't understand enums (by design?). qwaq's types.r now
compiles.
2012-11-19 13:49:34 +09:00
Bill Currie 3cffeb7b66 Handle assignment from void defs.
The main void defs are .return and .param_N. If the source operand is void,
use the destination operand's type to alias the source operand rather than
the source operand's type to alias the destination's operand (the usual
case).
2012-11-19 11:00:04 +09:00
Bill Currie 3d1792d2fc Properly count users of a tempory variable.
The dags code isn't the only place that creates temporary variables, so
count them as they go into a statement rather than when they're created.
This fixes the temp underflows.
2012-11-18 20:27:26 +09:00
Bill Currie 0c3aeb30aa Reuse tempary variables.
Now that I've got nice code, it was worth doing. Unfortunatly, bsearch
style switch statements have problems.
switch.r:14: BUG: temp users went negative: <tmp 0x21b6840:-1>
switch.r:14: BUG: temp users went negative: <tmp 0x21b6840:-2>
switch.r:14: BUG: temp users went negative: <tmp 0x21b6840:-3>
switch.r:14: BUG: temp users went negative: <tmp 0x21b67d0:-1>
2012-11-18 19:10:18 +09:00
Bill Currie c5fa4fe92a Generate code from the dag in topological sort order.
Nicely, the need for dag_gencode to recurse seems to have been removed.

At least for a simple case, correct code is generated :)

	switch.r:49:    case 1:         *to = *from++;
	003b loadbi.i *(from + 0), .tmp10
	003c add.i from, .imm, from
	003d storep.i .tmp10, *to
2012-11-18 13:10:19 +09:00
Bill Currie 1452dcca9a Create dependency edges.
A node that writes to a var must be evaluated after any node that reads
that var, so for any node reading var, add that node to the edges of the
node currently associated with the var (unless the node is a child of the
node reading the var).
2012-11-18 12:56:27 +09:00
Bill Currie d58595660f Treat newly created nodes as root nodes.
When a node is made a child of another node, it is removed from the dag's
set of root nodes.
2012-11-18 10:50:10 +09:00
Bill Currie af4e56bba8 Topologically sort the dag.
It doesn't make any difference yet, but that's because I need to add extra
edges indicating iter-node dependencies. However, the sort does seem to
work for its limited input.
2012-11-18 09:23:01 +09:00
Bill Currie 668871205a Add the child nodes to the node's edges set. 2012-11-17 22:12:03 +09:00
Bill Currie dcc786b16f Remove dead variables after creating the dag.
Not adding them while creating the dag completely broke the dag as
node(deadvar) always returned null. Code quality is back to where it was
before the dags rewrite.
2012-11-17 22:08:46 +09:00
Bill Currie deb5484c7e Move the node/label numbering into the alloc funcs.
I should have done it this way in the first place. much cleaner.
2012-11-17 21:55:59 +09:00
Bill Currie 12133bc02f Rewrite dags to use sets.
While things are quite broken now (very incorrect code is being generated),
the dag is much easier to work with. The dag is now stored in an array of
nodes (the children pointers are still used for dagnode operands), and sets
are used for marking node parents, attached identifiers and (when done,
extra edges).
2012-11-17 21:14:42 +09:00
Bill Currie 0bb2fc1891 Fix the lost line numbers on simple assignments.
Instead of storing the generating statement in the dagnode, the generating
expression is stored in the daglabel. The daglabel's expression pointer is
updated each time the label is attached to a node. Now I know why debugging
optimized code can be... interesting.
2012-11-17 12:07:27 +09:00
Bill Currie 43d77900d5 Rewrite dag_gencode to use statement/node types.
It now seems to generate correct code for each node. However, node order is
still incorrect in places (foo++ is being generated as ++foo). quattest.r
actually executes and produces the right output :)
2012-11-16 22:09:49 +09:00
Bill Currie bd5a790456 Support statement labels in dags.
While the code is broken in places, switch.r generates code again.
2012-11-16 20:12:13 +09:00
Bill Currie 4c6381d035 Mostly fix dag_create to work with flow_analyze_statement.
Label operands aren't handled properly, but dags are getting generated
again (better than before: return and goto are in).
2012-11-16 20:03:30 +09:00
Bill Currie 1100efba54 Nuke find_operands in favor of flow_analyze_statement.
flow_analyze_statement uses the statement type to quickly determin which
operands are inputs and which are outputs. It takes (optional) sets for
used variables, defined variables and killed variables (only partially
working, but I don't actually use kill sets yet). It also takes an optional
array for storing the operands: index 0 is the output, 1-3 are the inputs.
flow_analyze_statement clears any given sets on entry.

Live variable analysis now uses the sets rather than individual vars. Much
cleaner code :).

Dags are completely broken.
2012-11-16 19:33:37 +09:00
Bill Currie 001e1ac059 Classify statements into broad types.
The types are expression, assignment, pointer assignment (ie, write to a
dereferenced pointer), move (special case of pointer assignment), state,
function call/return, and flow control. With this classification, it will
be easier (less code:) to determine which operands are inputs and which are
outputs.
2012-11-16 16:16:06 +09:00
Bill Currie db605fa998 Improve the type accuracy of dag_gencode.
Surprisingly, I don't yet have to "throw one out", but things are still
problematic: rcall1 is getting two arguments, goto and return get lost,
rcall2 got an old temp rather than the value it was supposed to, but
progress :)
2012-11-15 17:58:33 +09:00
Bill Currie 3bfa89ca8b Clean up the operand type fixing.
It doesn't help any yet, but it will make things easier later on.
2012-11-15 17:15:34 +09:00
Bill Currie 012a521dc5 Strip aliases off daglabel operands.
However, the original type of the operand, as seen by the statement, is
kept. Mostly works, but has some problems with plistmenu.r
2012-11-15 15:53:51 +09:00
Bill Currie d76e2a8423 Make a start on regenerating code from dags.
Some parts are a lot messier than I thought, so the actual codegen in the
dags code is just a minimal stub for now.
2012-11-15 13:44:09 +09:00
Bill Currie 88b8bebe3c Detach dead temporary variables from dag nodes.
This allows temporary variables that are used in multiple nodes to remain
in the dag, but also will allow more freedom when generating code from the
dag.
2012-11-15 13:44:08 +09:00
Bill Currie fe13f21d9d Fix a segfault causing c&p error.
Found when I tried to skip attaching temporary vars to dag nodes (which
proved to be a bad move in the end, but exposing bugs is good).
2012-11-15 13:44:08 +09:00
Bill Currie 2b2ea5c9b3 Compute dags as part of data flow analysis.
I want to use the live variable information when building the dags.
2012-11-15 13:44:08 +09:00
Bill Currie 9f409b1ac4 Rename make_dag to dag_create. 2012-11-15 13:44:08 +09:00
Bill Currie 16410332af Create the dags list in execution order.
The root nodes of the dag need to be evaluated in execution order as some
roots may depend on the results of earlier roots (but then, this might also
be related to the problem of function calls not specifying all of their
parameters to the dag).
2012-11-15 13:44:08 +09:00
Bill Currie 04a8e19481 Move the flow-var use/define calcs to a better place.
dags.c really isn't the right place. The only reason it wasn't in flow.c in
the first place is find_operands was in the wrong place.
2012-11-15 13:44:08 +09:00