Commit Graph

2868 Commits

Author SHA1 Message Date
Bill Currie 1e72e3eb20 Make the post-optimization sblocks dump optional. 2012-11-19 15:24:09 +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 f2cc82469a Use the correct field when dechaining aliases.
op_type and type always confuse me :P
2012-11-19 12:53:08 +09:00
Bill Currie c03511734d Fix a dreaded uninitialized variable.
Sneaky bastard :P (typo)
2012-11-19 12:52:24 +09:00
Bill Currie 674c823547 Set the operand type for op_label.
It was never set before (and thus void), but as it now needs to be
ev_short, set it when getting the fake def used for label operands.
2012-11-19 12:30:23 +09:00
Bill Currie 5018f5147c Use operand types rather than def types to select opcodes.
With temp types changing and temps being reused within the one instruction,
the def type is no longer usable for selecting the opcode. However, the
operand types are stable and more correct.
2012-11-19 12:28:41 +09:00
Bill Currie 60321061d2 Fix dot_expr.c to work with dump_dot(). 2012-11-19 12:14:02 +09:00
Bill Currie e70b59b925 Don't null the def pointer when freeing a temp.
It makes debugging more difficult.
2012-11-19 11:02:31 +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 158e261a8f Give reusable temps their own linked list.
The next field is used for linking all defs in a space. Oops.
2012-11-19 10:33:03 +09:00
Bill Currie 1af4d5eeed Ensure reused temps have the correct type.
The type of a temp may change through its life as it's really the location
and name that are reused.
2012-11-19 10:31:22 +09:00
Bill Currie 4e8e94a1b2 Detect aliased temopary variables.
This takes care of some temps not being freed.
2012-11-18 20:29:40 +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 3897142038 Add a couple more tests to ptrderef.r 2012-11-18 13:28:46 +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 8fe71f2146 Display a dagnode's extra dependency edges. 2012-11-18 12:55:44 +09:00
Bill Currie d3f010a81b Fix the flow-dag connections in the flow graphs. 2012-11-18 11:34:01 +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 872b13616d Ensure return's operand gets set properly.
Yay for coding and designing at the same time, not :P
2012-11-16 21:50:27 +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 b8b626ee81 Change the int<->float conversion opcode string.
Using "=" was rather confusing, so changing it to "<CONV>" seems to be a
good idea. As the string is used only for selecting opcodes at compile
time, only qfcc is affected.
2012-11-16 14:45:11 +09:00
Bill Currie c1776fc2ad Make the dags in flow graphs a little easier to read. 2012-11-16 12:42:57 +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 c1a5c1a7b2 Create alias_operand (). 2012-11-15 15:52:30 +09:00
Bill Currie 095210893f Ensure alias operands are never nested.
No more than one level of aliasing is ever needed, so strip off any
intervening aliases that show up.
2012-11-15 15:18:00 +09:00
Bill Currie 6621a42da9 Better handling of progs.dat and line numbers.
Not enough testing :P
2012-11-15 14:53:09 +09:00
Bill Currie 1c32ac8ce6 Expose new_statement, too. 2012-11-15 13:44:09 +09:00
Bill Currie 69543cf85b Fix an uninitialized variable.
It was never a propblem until an attempt to dump empty sblocks was made.
2012-11-15 13:44:09 +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 802e1981bb Expose some statements functions needed for code gen. 2012-11-15 13:44:09 +09:00
Bill Currie c43a8331cf Expose dump_dot_sblock. 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 c358a0e77e Create a function to generalize dot dumping.
Now, any time a graph is wanted, the file can be consistently named without
a lot of messy code.
2012-11-15 13:44:08 +09:00
Bill Currie 5051c922a5 Ensure alias operands are properly freed. 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 e77d3b9e54 Fix a missed colspan in sblock dumps. 2012-11-15 13:44:08 +09:00
Bill Currie 783b4082a1 Produce tidier dag dumps. 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
Bill Currie c4d4174ebc Make some minor improvements to flow graph dumps. 2012-11-15 13:44:08 +09:00
Bill Currie 3298e4d67e Ensure find_operands sets the operands to known values.
This fixes the last of the bogus live variables.
2012-11-15 13:44:08 +09:00
Bill Currie e3a49df0a8 Set def after setting use.
An instruction that both reads and writes the same variable will read the
variable before writing to it, so the instruction uses the variable rather
than defines it (for live-variable purposes).
2012-11-15 13:44:08 +09:00
Bill Currie 58cb73d6aa Remember to union the node's use with (out - def)
Oops.
2012-11-15 13:44:08 +09:00
Bill Currie cd224c00d4 Add live variable info to flow graph dumps.
Things are a little bogus, but at least the bogosity is visible :)
2012-11-15 13:44:08 +09:00
Bill Currie 8817c4a1c2 Re-enable the flow graph dumping.
It's still in a bogus place, but it's necessary.
2012-11-15 13:44:08 +09:00
Bill Currie 3c79f07705 Fix an out-by-one error in the set operators.
I have no idea why I added that - 1.
2012-11-15 13:44:08 +09:00
Bill Currie 4b64912793 Begin doing some data flow analysis.
In te beginning, live variables.
2012-11-15 13:44:08 +09:00
Bill Currie c00e666668 Expose find_operands.
It has proven to be a generally useful function, not just for dags.
2012-11-15 13:44:08 +09:00
Bill Currie 0a2ce6d381 Fix some doxygen comments. 2012-11-15 13:44:08 +09:00
Bill Currie 5514c8db3c Print the statement number in the sblock output. 2012-11-15 13:44:08 +09:00
Bill Currie 0b49bd343c Collect information on statements that set or use vars.
First, it turns out using daglabels wasn't such a workable plan (due to
labels being flushed every sblock). Instead, flowvars are used. Each actual
variable (whether normal or temp) has a pointer to the flowvar attached to
that variable.

For each variable, the statements that use or define the variable are
recorded in the appropriate set attached to each (flow)variable.
2012-11-15 13:44:08 +09:00
Bill Currie 7404aacab7 Resurrect dags building and printing.
However, this time it's done after the statement recording (so the
statement numbers are set).
2012-11-15 13:44:08 +09:00
Bill Currie acedc65de1 Make an array of statements for the function.
With this, sets can be used to represent statements.
2012-11-15 13:44:07 +09:00
Bill Currie 98245f6d00 Bury a dead field. 2012-11-15 13:44:07 +09:00
Bill Currie 8844ac61a2 Rename dump_flow to dump_sblock and move to dot_sblock.c 2012-11-15 13:44:07 +09:00
Bill Currie d6d03a63b4 Print the depth-first order of the flow graph nodes. 2012-11-15 13:44:07 +09:00
Bill Currie acc3beb6ab Make flow_find_dominators a little more robust.
While a non-initial node not having predecessors is really an internal
error, segfaulting because of it is not nice.
2012-11-15 13:44:07 +09:00
Bill Currie acde6ca0a0 Handle jumpb statements in dot_sblock and flow graphs. 2012-11-15 13:44:07 +09:00
Bill Currie ffa08a99c2 Store the initializer expression in the def.
Initially, this will be used for collecting the targets of a jumpb
instruction.
2012-11-15 13:44:07 +09:00
Bill Currie d7177a78e9 Rewrite the flow graph code.
The flow graph nodes are now properly separated from the graph, and edge
information is stored in the graph struct. This actually made for much
cleaner code (partly thanks to the use of sets and set iterators).

Flow graph reduction has been (temporarily) ripped out as the entire
approach was wrong. There was also a bug in that I didn't really understand
the dragon book about selecting nodes and thus messed things up. The
depth-first search tree "fixed" the problem, but was really the wrong
solution (sledge hammer :P).

Also, now that I understand that dot's directed graphs must be acyclic, I
now have much better control over the graphs (back edges need to be
flipped).
2012-11-15 13:44:07 +09:00
Bill Currie b9599a7119 Better set memory management.
At the cost of one pointer per set, sets can now be allocated and freed
more efficiently (especially since malloc might round things up).
2012-11-15 13:44:07 +09:00
Bill Currie 2385cf65c2 Rename setstate_t to set_iter_t.
After all, it is an iterator :).
2012-11-15 13:44:07 +09:00
Bill Currie d3d88290b5 Mostly satisfactory output of nested regions.
It turns out dot does not like cyclic graphs (thus some of the weird
layouts), but fixing it by flipping back-edges requires proper recording of
edge info (I guess that's what T is for in the dragon book).
2012-11-15 13:44:07 +09:00
Bill Currie 8c5a2b3d11 Implement depth-first searching for flow graphs.
This fixes the bogusly irreducible graphs.
2012-11-15 13:44:07 +09:00
Bill Currie 53622dbb99 Perform graph reduction on the function's flow graph.
The reduction is performed itteratively until the graph is irreducible, but
such that each reduction wraps the previous graph. Unfortunately, due
depth-first searching not being implemented, graphs that should be reduced
(ie, those with natural loops).
2012-11-15 13:44:07 +09:00
Bill Currie 388b6e6e8f Add set_size ().
Return the number of members in the set.
2012-11-15 13:44:07 +09:00
Bill Currie 8567508963 Rewrite set_first() and add set_next().
set_first() now returns a pointer to a setstate_t struct that holds the
state necessary for scanning a set. set_next() will automatically delete
the state block when the end of the set is reached. set_delstate() is also
provided to allow early termination of the scan.
2012-11-15 13:44:07 +09:00
Bill Currie 2da038ce0f Bring back dot_flow.c.
However, it produces simplified flow graphs (ie, just nodes and edges, no
statements, though later on data flow information will be added).
2012-11-15 13:44:07 +09:00
Bill Currie 8b374305d2 Rename dot_flow.c and print_flow.
They're now dot_sblock.c and print_sblock. The new names both better
reflect their purpose and free up "flow" for outputting the real flow
analysis graphs.
2012-11-15 13:44:07 +09:00
Bill Currie c483770121 Rework flow.c with the aim of graph reduction.
Much of the data recently added to sblock_t has been moved to flownode_t.
No graph reduction is carried out yet, but the initial (innermost level)
graph has been built.
2012-11-15 13:44:07 +09:00
Bill Currie da446fd343 Detect natural loops.
Non-nested loops that have the same head-node are merged into one loop.
However, loop nesting is not currently detected (next step).
2012-11-15 13:44:07 +09:00
Bill Currie 8fb958603c Add set_first.
It returns the first element of a set. If the set is empty, -1 (unsigned)
is returned.
2012-11-15 13:44:07 +09:00
Bill Currie 4cc8d4a04b Fix an apparent snafu in set_test.
It looks like a bit of case&paste where I forgot to change != to &.
2012-11-15 13:44:07 +09:00
Bill Currie 1fead50f4e Calculate the dominators of each node in the flow graph.
The dominators are represented by sets using the node numbers.
2012-11-15 13:44:07 +09:00
Bill Currie 964ea7f9fe Add set_as_string for debug output. 2012-11-15 13:44:07 +09:00
Bill Currie d2fab28d01 Implement set_is_member.
Oops, forgot that one.
2012-11-15 13:44:06 +09:00
Bill Currie 7853bf1859 Calculate a node's successors and predecessors.
The dot graphs are a little odd (arrow heads on the wrong end of the
predecessor edges), but things seem to be correct.
2012-11-15 13:44:06 +09:00
Bill Currie 739ebc522c Begin working on flow analysis.
For now, variable and flow graph node maps are built.
2012-11-15 13:44:06 +09:00
Bill Currie f30010b367 Add some docs to function.h 2012-11-15 13:44:06 +09:00
Bill Currie 5acf474882 Double quote dag labels.
Dot interprets escape sequences in non-html string, and needs the quotes to
be escaped, so quote the result of operand_string. Unfortunately,
operand_string uses quote_string and quote_string returns a static pointer,
so some hoop-jumping is necessary.
2012-11-15 13:44:06 +09:00
Bill Currie cdc790d0c8 Add a set type and support code. 2012-11-15 13:44:06 +09:00
Bill Currie 60d03dbdd9 Fix the naive opcode checks.
Confusing . with .=, and < and <= with more <MOVE etc isn't good.
2012-11-15 13:44:06 +09:00