Commit graph

2938 commits

Author SHA1 Message Date
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
388247791d Re-enable the dags specific flow graph dump. 2012-11-30 16:56:25 +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
c4ba294433 Fix the icky fixme :) 2012-11-27 20:32:52 +09:00
Bill Currie
a91f4cedeb Add the @nil keyword.
@nil is exactly the same as nil, but it is always available.
2012-11-27 18:33:42 +09:00
Bill Currie
edeffd64e9 Fix a missed obj_module_s/_t from the class cleanup.
Doesn't seem to have caused any problems, but still :)
2012-11-27 18:33:42 +09:00
Bill Currie
026a992179 Add a test case for the alias chain mangling.
I think I need to rework the tests to better handle compile-only tests.
2012-11-27 18:33:41 +09:00
Bill Currie
31b07bcbbf Rewrite expr_alias().
Modifying the existing alias chain proved to be a bad idea (in retrospect,
I should have known better:P). Instead, just walk down any existing alias
chain to the root operand and build a new alias from that.
2012-11-27 18:33:41 +09:00
Bill Currie
215ff61216 Add a test for the dead boolean expression.
This tests the zombie label problem found via ctf.
2012-11-26 21:05:24 +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
e28aa0a5fb Fix some errors in the set code.
I really need to write some unit tests for the set code, but with this (and
an uncommitted dags patch (still thinking about it)) ctf compiles.
2012-11-26 17:19:02 +09:00
Bill Currie
8e225cd726 Fix a missing label use count decrement.
if statements in dead code weren't being removed due to the label.
2012-11-26 16:13:59 +09:00
Bill Currie
24847857ef Fix some whitespace. 2012-11-26 16:13:40 +09:00
Bill Currie
128547b885 Resurrect the "next" pointers for labels and conditionals. 2012-11-26 16:09:53 +09:00
Bill Currie
f7417345ba Rewrite print_bool to be more informative. 2012-11-26 15:47:10 +09:00
Bill Currie
147d00d41f Rewrite print_block making it more informative. 2012-11-26 15:04:33 +09:00
Bill Currie
ce1d89ba50 Replace some of the quotes around node names.
Null expression pointers can show up unexpectedly, and printf's (null)
messes up dot.
2012-11-26 14:18:23 +09:00
Bill Currie
578ad07487 Fix yet another empty block issue.
I think I need to rework all the code in there, but this will do for now.
2012-11-25 15:01:36 +09:00
Bill Currie
f6e5428382 Don't create default_expr unless needed.
The goto for the default expression is the source of the mis-counted label
users: the label was being counted by the goto, but the goto was never
being inserted into the code (only v6 progs or "difficult" types insert the
goto).
2012-11-24 21:53:00 +09:00
Bill Currie
d0e56c91cc Detect non-initial nodes with no predecessors.
Such nodes are unreachable code (ie, dead blocks), but the dead block
removal code failed to remove them (current known cause: miscounted label
userrs). As such blocks cause problems for data flow analysis, ignoring
them is not a good idea. Thus make them an internal error.
2012-11-24 21:39:02 +09:00
Bill Currie
4f8b3b5692 More empty block checks. 2012-11-24 19:06:10 +09:00
Bill Currie
6e7aaac1b9 Fix the wording of a comment. 2012-11-24 19:06:01 +09:00
Bill Currie
b68401e552 Produce a debug print when using jump tables. 2012-11-24 17:55:57 +09:00
Bill Currie
4c1dc568e0 Cop out on uninitialized large variables.
vectors, quaternions and structs are a little tricky. I need to think about
how to get them working, but I also want qfcc to get through as much code
as possible.
2012-11-24 16:49:46 +09:00
Bill Currie
40c97f9570 Tread immediate move statements similarly to assignments.
They're used for structure assignment in near data space. This fixes the
bogus uninitialized warnings on fully initialized structs.
2012-11-24 16:47:44 +09:00
Bill Currie
7f229b7682 Remove execessive html quoting.
Heh, I must have put that in long before I did the quoting functions.
2012-11-24 15:42:21 +09:00
Bill Currie
dbbca47a3b Mark initialized struct/array defs as initalized.
Also, use the initialized flag for setting up the initalized var set in
flow_uninitialized().
2012-11-24 15:26:27 +09:00
Bill Currie
f4604d9db5 Remove gotos that go to the following statement.
eg:
	goto foo:
foo:
	//some code
2012-11-24 15:08:48 +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
2478b053de Add a test for writes to global vars in a loop. 2012-11-24 14:53:45 +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
bc26cd24c8 Make structptr.r a little quieter. 2012-11-22 21:59:01 +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
64d9bbd230 Make the failure message clearer. 2012-11-22 21:59:01 +09:00
Bill Currie
b920a4dec9 Add a new test case.
It turns out that the optimizer completely breaks (a + b) % (a - b) with v6
code.
2012-11-22 21:59:01 +09:00
Bill Currie
47decd58d0 Support testing v6 progs.
v6 progs return float rather than int (they don't have int).
2012-11-22 21:59:01 +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
538879b451 Get automatic dependencies working for the progs files.
Including dependencies on QFCC itself :)
2012-11-22 21:59:01 +09:00
Bill Currie
92d2b4107c Make the --developer and --trace options work. 2012-11-22 21:59:01 +09:00
Bill Currie
a5a7780a21 Fix make dist for qfcc's test. 2012-11-22 21:59:01 +09:00
Bill Currie
99b34232f8 Add a test harness for qfcc.
It's only the beginning, but finally make check is useful for qfcc :)
It even has a failing test :D
2012-11-22 21:59:01 +09:00
Bill Currie
95bd4ad410 Add a test for while (count--) 2012-11-22 21:59:00 +09:00
Bill Currie
49ca09a64f Clean up qfcc's test directory.
I don't remember what half these tests were for :/
2012-11-22 21:58:52 +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
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
c759b73b24 Cope with empty sblocks when producing dot graphs. 2012-11-21 12:23:15 +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
0583dc82d3 Treat uninitialized temps as a bug.
It really should be impossible, but I'm not sure where the bug is yet
(though there are uninitialized variables that are false positives that
most definitely are initialized, might be related)
2012-11-21 10:26:41 +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
686937123c Ensure flowvars always point to the var itself.
Pointing to aliases of the var causes all sorts of problems, but this time
it was causing the uninitialized variable detector to miss certain
parameters.
2012-11-21 09:43:59 +09:00
Bill Currie
0dc306c4b7 Fix the initialized var data flow.
Now most false positives have been fixed. Just some problems with aliased
vars (next) and then the usual false positives caused by branches.
2012-11-21 09:42:02 +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
a734feacfd Create the .parN variables for varargs functions.
Ouch, 2 years and this is the first test?
2012-11-20 17:14:58 +09:00
Bill Currie
984aa9a4cf Support entity.field = structvar2 = structvar1;
The structvar2 = structvar1 is implemented as a move expresion, which
address_expr didn't like. Return the address of the source. For indirect
move expressions, this is just the source expression itself.
2012-11-20 17:12:16 +09:00
Bill Currie
5cfeb7809f Fix a missing ; 2012-11-20 17:11:39 +09:00
Bill Currie
b91bb2e915 Add direct moves to expression dumping. 2012-11-20 16:55:19 +09:00
Bill Currie
793112de9a Add some more tests.
structptr.r is actually usable for automated testing when I get around to
it.
2012-11-20 15:39:00 +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
06a62be1d5 Fix redundant constant values.
I had forgotten to add the new value to the hash table. Oops.
2012-11-20 13:33:57 +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
0dea564cb4 Be more careful of empty sblocks.
Accessing the final statement of an sblock via tail doesn't work in an
empty sblock because tail points to sblock->statements and thus the cast is
invalid. This bug has be lurking for a long time, but for some reason the
cse stuff tickled it (thankfully!!!).
2012-11-19 16:11:07 +09:00
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