Commit graph

638 commits

Author SHA1 Message Date
Bill Currie
2dc60d4f9a Allow tests for full overlap of defs.
Not actually used yet, but I imagine I'll want it.
2012-12-13 14:42:51 +09:00
Bill Currie
c61e03a0b9 Clean up operand creation.
Rather than having the creation scattered through the code, use helper
functions. Makes exposing operand creating saner.
2012-12-13 12:49:00 +09:00
Bill Currie
204a0b3f72 Make def_visit_all return the actual result of visit.
This way, def_visit_all is a little more useful (though I think I might
redo the one case that's using this feature).
2012-12-13 09:47:00 +09:00
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
8582e9de63 Make it possible to dump statement blocks in a flow graph.
Much nicer looking with proper back edges :). Not actually enabled, though.
2012-12-11 11:31:55 +09:00
Bill Currie
3aa3a0945a Scan the statements in a node with suspect var usage.
When the naive uninitialized variable detection finds a node with possible
uses of uninitialized variables, the statements in the node are scanned one
at a time checking each usage and removing uninitialized definitions as
appropriate. vectest.r now compiles without warnings. As an added bonus,
accurate line number information is reported for uninitialized variables.

Unfortunately, there is still a problem with uninitialized temps in
switch.r, but that might just be poor handling of temp op aliases.
2012-12-10 20:49:08 +09:00
Bill Currie
2e105f17f2 Enable live var flow dumps. 2012-12-10 15:27:07 +09:00
Bill Currie
8f274f5b1d Add convenience functions for getting a def's offset and size. 2012-12-10 14:40:43 +09:00
Bill Currie
d717a0b3f2 Add dummy nodes at the beginning and end of the graph.
The dummy nodes are for detectining uninitialized variables (entry dummy)
and making globals live at function exit (exit dummy). The reaching defs
and live vars code currently seg because neither node has had its sets
initialized.
2012-12-10 13:56:26 +09:00
Bill Currie
4f70b48370 Add a function to check if two defs overlap.
Very useful for alias handling :)
2012-12-10 13:23:45 +09:00
Bill Currie
fa45ab842f Make the reaching defs dot dump optional. 2012-12-10 12:17:20 +09:00
Bill Currie
e4dd86c36b Calculate reaching defs. 2012-12-09 22:37:59 +09:00
Bill Currie
1c99cf50da Clean up the flow api a little. 2012-12-09 20:50:53 +09:00
Bill Currie
054d902d3a Document alias defs.
The diagram showing the basics of how alias defs work is in a spearate file
because it created to much clutter in the header file.
2012-12-09 19:43:12 +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
b28ac6672b Add support for set of everything, and a lot of docs.
Set of everything is implemented by inverting the meaning of bits in the
bitmap: 1 becomes non-member, 0 member. This means that set_size and
set_first/set_next become inverted and represent non-members as counting
members becomes impossible :)
2012-12-06 19:32:31 +09:00
Bill Currie
b6ae9867c2 Fully connect temps and their aliases. 2012-12-06 09:40:16 +09:00
Bill Currie
7071a46936 Make convert_value create a new value.
It operating in-place proved to be troublesome.
2012-12-05 22:16:08 +09:00
Bill Currie
a099847026 Do a little bit of const correctness for type.[ch]
It's only the beginning :/
2012-12-05 22:15:19 +09:00
Bill Currie
08b27efe33 Nuke the return type flow calculations.
They've proven to be unnecessary with the recent symbol/def changes.
2012-12-05 19:56:10 +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
3bb8b1b9d2 Make it easy to print symbol types. 2012-12-05 19:45:16 +09:00
Bill Currie
cc88202f8c Fix function.h's doxgygen grouping. 2012-12-05 16:01:28 +09:00
Bill Currie
0585471723 Make defspaces typed.
Simply "backed" and "virutal". Backed spaces have memory allocated to them
while virtual spaces do not. Virtual spaces are intended for local
variables and entity fields.
2012-12-04 14:16:52 +09:00
Bill Currie
d340aac2eb Fix the bugs marked in defspace.h
Now size is checked properly for defspace_free_loc and defspace_alloc_loc,
and defspace_alloc_loc check's grow()'s return value.
2012-12-04 13:40:00 +09:00
Bill Currie
98a0afa38f Add and use FREE() to complement ALLOC().
Now it will be easy to test memory access with valgrind (just compile with
DEBUG_QFCC_MEMORY defined).
2012-12-04 13:23:31 +09:00
Bill Currie
0872961269 Do a little bit of cleanup. 2012-12-04 12:48:52 +09:00
Bill Currie
7412a45c65 Attach alias defs to their base def.
With this, alias defs become singletons based on the def they alias and the
type and offset of the alias. Thus, the removal of the free_def call in
emit.c.
2012-12-03 12:40:00 +09:00
Bill Currie
919ea8192a Make alias_def take an offset.
alias_def now always creates an offset def (though the usual case has an
offset of 0). The if the alias escapes the bounds of the base def, an
internal error will be generated.
2012-12-03 11:50:26 +09:00
Bill Currie
9d0332ae30 Make alaising a def to a larger type an internal error.
It really doesn't seem wise to allow the compiler to do so as it would
overwrite unrelated defs. The only time such a thing is valid is the return
statement (silly vm design), and that's read-only.
2012-12-03 11:38:55 +09:00
Bill Currie
cd64c0ad9b Document defspace.h 2012-12-02 22:04:54 +09:00
Bill Currie
3ecb7e8805 Mised some st_->sc_ changes.
Hiding in a comment in a header, sneaky rats ;)
2012-12-02 22:00:07 +09:00
Bill Currie
e3bf64262f Remove the "data" defspace from pr_info_t.
It's only usage would have caused a segfault if it had been hit. I suspect
it's a holdover from before the rewrite of 2011.
2012-12-02 20:09:54 +09:00
Bill Currie
72108ba0b5 Document def.h.
Also remove the extern for current_storage as it belongs in shared.h.

I'm not satisfied with the documentation for initialize_def, but it will do
for now. I probably have to rewrite the thing as it's a bit of a beast.
2012-12-02 19:54:20 +09:00
Bill Currie
5725c5124c Rename the storage_class_t enum values.
With the intoduction of the statement type enum came a prefix clash. As
"st" makes sense for "statement type", I decided that "storage class"
should be "sc". Although there haven't been any problems as of yet, I
decided it would be a good idea to clean up the clash now. It also helps
avoid confusion (I was a bit surprised after working with st_assign etc to
be reminded of st_extern etc).
2012-12-02 10:11:30 +09:00
Bill Currie
54289da416 Fix the line number info for else.
It has bugged me for a while that the goto for else would display the if's
source line.
2012-12-01 16:37:38 +09:00
Bill Currie
d47fa0fc89 Make the new optimizations optional.
The usual -O :) (no numbers yet, though). Alternatively, -C [no-]optimize
may be used.
2012-12-01 11:13:45 +09:00
Bill Currie
848493379d Support calls through function temps.
I had forgotten function vars stored in ents and objects would use a temp
when calling the function.
2012-12-01 11:10:47 +09:00
Bill Currie
554b2e4710 Add flow analysis to determin the type of .return.
It doesn't quite work yet, but...

It has proven necessary to know what type .return has at any point in the
function. The segfault in ctf is caused by the return statement added to
the end of the void function messing with the expr pointer stored in the
daglabel for .return. While this is actually by design (though the
statement really should have a valid expr pointer rather than), it actually
highlights a bigger problem: there's no stable knowledge of the current
type of .return. This is not a problem in expression statements as the
dagnodes for expression statements store the desired types of all operands.
However, when assigning from .return to attached variables in a leaf node,
the type of .return is not stored anywhere but the expression last
accessing .return.
2012-11-30 17:15:05 +09:00
Bill Currie
71d1804e22 Clean up flow diagrams.
Now information like dags or live variables are dumped separately, and the
live variable information replaces the flow node in the diagram (like dags
have recently).
2012-11-30 16:52:22 +09:00
Bill Currie
93f53605ed Move and rename statement related flow_is_* and flow_get_*
They really should have been in statements.[ch] in the first place
(actually, they sort of were: is_goto etc, so some redundant code has been
removed, too).
2012-11-30 14:06:52 +09:00
Bill Currie
086fd0f3c3 Correct a comment. 2012-11-30 13:57:08 +09:00
Bill Currie
d6b38dd0ee Add an option to control expr tree dumping.
About bloody time :P
2012-11-26 21:00:28 +09:00
Bill Currie
eef399209d Add some very evil pragmas.
The evil comment is not just "pragmas are bad, ok?", but switching between
advanced, extended and tradtitional modes when compiling truly is evil and
not guaranteed to work. However, I needed it to make building test cases
easier (it's mostly ok to go from advanced to extended or tradtional, but
going the other way will probably cause all sorts of fun).

In the process, opcode_init now copies the opcode table data rather than
modifying it.
2012-11-22 21:59:01 +09:00
Bill Currie
1d34da26f7 Add an option to dump frame files.
After running across a question about lists of animation frames and states,
I decided giving qfcc the ability to generate such lists might be a nice
distraction from the optimizer :) Works for both progs.src and separate
compilation. No frame file is generated if no macros have been created.
2012-11-21 20:50:45 +09:00
Bill Currie
c5f2e3c4ad Make dag dumping from gdb easier. 2012-11-21 15:03:44 +09:00
Bill Currie
bedde02307 Make flow graphs with dags easier to read.
The node contents are now the dag rather than the dag being attached to the
node.
2012-11-21 13:43:03 +09:00
Bill Currie
86968f662e Store the function type in function values.
This fixes IMP msg = nil;
2012-11-21 10:06:15 +09:00
Bill Currie
547ea78ad7 Implement uninitialized variable checking.
It currently gets a lot of false positives (globals and parameters?), but
the initial tests for just local vars worked.
2012-11-20 21:49:47 +09:00
Bill Currie
d8a5d6745f Add a param flag to defs.
It is necessary to know if a def is a function parameter so it can be
treated as initialized by the flow analyzer. The support for the flag in
object files is, at this stage, purely for debugging purposes.
2012-11-20 17:50:59 +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
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
1e72e3eb20 Make the post-optimization sblocks dump optional. 2012-11-19 15:24:09 +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
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
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
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
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
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
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
1c32ac8ce6 Expose new_statement, too. 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
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
9f409b1ac4 Rename make_dag to dag_create. 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
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
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
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
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
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
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