Commit graph

4533 commits

Author SHA1 Message Date
Bill Currie
fc03e7eb7a [qfcc] Recursively expand __VA_OPT__'s argument
This seems to do the right thing, and my test case (copied from the C
spec) passes finally.
2023-11-18 18:09:34 +09:00
Bill Currie
344b79a401 [qfcc] Go back to not pre-expanding macro args
It just feels cleaner than unnecessarily copying token chains. It turns
out that the core problem was just order of operations in next_token:
moving the pending_macro code to after arg/macro detection seems to be
correct (even bare `G LPAREN() 0)` is *not* expanding `G`, as expected).
2023-11-18 17:37:20 +09:00
Bill Currie
d628595589 [qfcc] Collect __VA_OPT__'s arguments early
This makes working with __VA_OPT__ much easier. However, I've come to
dislike expand_args. Still, the code is slowly cleaning up.
2023-11-18 13:41:20 +09:00
Bill Currie
114f564a98 Fix some issues found by gcc's -flto
More const/pure stuff and even some type issues.
2023-11-16 17:09:20 +09:00
Bill Currie
748bd377c6 [qfcc] Initialize overloaded function tables early
This fixes a rather old segfault bug uncovered by the macro testing.
2023-11-16 01:18:10 +09:00
Bill Currie
e86444eff8 [qfcc] Expand macro args early
This gets macro expansion mostly working, but __VA_OPT__ isn't expanding
properly yet. Still, one step closer.
2023-11-16 01:16:39 +09:00
Bill Currie
1dc5d4990c [qfcc] Expand __VA_OPT__ early
__VA_OPT__ needs to be fully expanded before it can be processed as an
argument. Unfortunately, this has uncovered bugs elsewhere in macro
expansion.
2023-11-15 12:46:38 +09:00
Bill Currie
37819523d0 [qfcc] Fix macro expansion basics (again)
It turned out I had simply forgotten to ensure the token chains were
properly terminated (the struct copy would copy the next of the source
token and thus macro args always expanded to the last token of the
parent macro). And then I'd missed saving the token text when parsing
predefined macros. __VA_OPT__ is still a problem, but this work was for
making that a little easier.
2023-11-12 13:20:35 +09:00
Bill Currie
8de214c782 [qfcc] Rework preprocessor tokens
I got tired of the way the separate token types for macro expansion and
the rest of the preprocessor parser were handled. This makes them a
little more unified. Macro expansion seems to be slightly broken again
in that min/max/bound mess up badly, and __VA_OPT__ does things in the
wrong order, but I wanted to get this in as a checkpoint.
2023-11-11 22:32:29 +09:00
Bill Currie
881b6626e4 [qfcc] Partially implement __VA_OPT__ and __VA_ARGS__
__VA_ARGS__ seems to be working but __VA_OPT__ still needs a lot of work
for dealing with its expansions, but basic error checking and simple
expansions seem to work.
2023-11-07 12:36:56 +09:00
Bill Currie
d21260d9f6 [qfcc] Use rua_loc_t for most location information
This gets rid of the simple source_file and source_line in pr_info_t, so
all expressions, and many other things have full location information.
2023-11-06 14:25:20 +09:00
Bill Currie
bd2bc16767 [qfcc] Get token##pasting working
This seems to do the right thing, but I need to come up with a good test
suite.
2023-11-06 11:28:17 +09:00
Bill Currie
b952c11dce [qfcc] Get function-type macro arguments working
Or at least mostly so: token##pasting is still broken, but #stringize
and nested macros seem to work.
2023-11-04 21:08:40 +09:00
Bill Currie
6bfb1f37f0 [qfcc] Rework macro expansion
Macros now store their arguments and have a cursor pointing to the next
token to take from their expansion list. While not checked yet, this
will make avoiding recursive macro invocations much easier. More
importantly, it's a step closer to correct argument expansion (though
token pasting is currently broken).
2023-11-03 13:54:08 +09:00
Bill Currie
6408c131dd [qfcc] Ensure macros expand for #elif
It seems I wasn't quite paying attention to when I should be getting
that invalid directive error for #version and when I shouldn't.
2023-11-03 13:48:38 +09:00
Bill Currie
82ce36c981 [qfcc] Add macro support for __FILE__ and __LINE__
There are some expansion problems, but this moves their handling to
where it needs to be.
2023-11-01 16:19:39 +09:00
Bill Currie
bbae71c488 [qfcc] Save and reset condition stack for includes
This makes it much easier to keep track of end of file in a conditional
block (#if...#endif) as #include in non-suppressed code would result in
spurious eof errors otherwise. I'm a little concerned about correctness,
but everything seems to work and it should be right as suppressed
include directives do not change the state at all, and the suppressed is
its own flag not in the condition stack.
2023-10-31 10:48:11 +09:00
Bill Currie
c5b357ddb1 [qfcc] Delay processing of asx and incop tokens
The op code needs to be set just before being passed to the qc parser so
it doesn't get lost in macro expansion.

And vector values need to not be processed when recording otherwise they
get lost.
2023-10-31 10:38:09 +09:00
Bill Currie
51f3915e12 [qfcc] Separate preprocess only and preprocess output
-M does only preprocessing but does not generate the preprocessed output
(which is -E's job). Prevents mangled dependency files.
2023-10-31 08:42:28 +09:00
Bill Currie
b3cbec7f87 [qfcc] Add the preprocessor files to BUILT_SOURCES
Intermittent build failures are no fun.
2023-10-30 23:17:08 +09:00
Bill Currie
9a00998d9c [qfcc] Fix vector ops after preprocessor changes
I'm not sure what exactly caused vector literals to break, but bailing
out of the vector ops section on conversion to vector or quaternion
fixes game-source.
2023-10-30 22:46:43 +09:00
Bill Currie
05eea82d0d [qfcc] Relax int conversion criteria
Allow 32-bit positive values without a warning and warn on conversion
issues for float. The whole conversion system needs cleaning up for
v6/v6p/ruamoko.
2023-10-30 18:52:02 +09:00
Bill Currie
5f21422df3 [qfcc] Parse parameterless function-type macros
This allows `#define foo()` to work as expected.
2023-10-30 17:20:32 +09:00
Bill Currie
16241b6811 [qfcc] Fix a bunch of preprocessing options issues
-D options weren't counting correctly so build_cpp_args was writing past
the end of the array allocated for command line arguments

parse_cpp_name had an out-by-one resulting in reading past the end of
the string.

The qfcc system include path was being set in the wrong place (not sure
why I thought that was right), and not respecting no_default_paths.

-M was generating preprocessor output when it should not have been,
resulting in corrupted dependency files.
2023-10-30 17:16:16 +09:00
Bill Currie
c7905422f1 [qfcc] Use a hash table for types
I don't remember why I couldn't get this to work last time I tried, but
it went well this time, and made a significant difference to compiling
vulkan.r (from 1.1s to 0.15s unoptimized or 0.8s optimized).
2023-10-30 15:38:43 +09:00
Bill Currie
bdf3316bef [qfcc] Simplify // comment handling
This fixes the problem with // comments after the file in #include and
the core problem the complicated // handling tried to fix with
suppressed directives. Funny how it's always the simpler code that works
better :/
2023-10-30 14:32:11 +09:00
Bill Currie
a3c94628bc [qfcc] Detect errors in module finalization
Though they shouldn't happen (they're really internal errors), it's not
good pretending they didn't happen.
2023-10-30 13:51:45 +09:00
Bill Currie
98a2cb7505 [qfcc] Update defines for back-end target
That is, `__RUAMOKO__` and `__VERSION6__` were missed when I got `-D`
working with the built-in preprocessor.
2023-10-30 13:32:20 +09:00
Bill Currie
ab34cb7df4 [qfcc] Accept @id as one token in macro bodies
I'm undecided about @ in macro names, but treating @id as one token in
the body is necessary with the single-pass tokenizing. Fixes an infinite
macro expansion loop in vecaddr.r (`#define dot @dot`), but that's
really only a bandaid for *that* issue as there are plenty of other
cases where macros will loop.
2023-10-30 13:28:45 +09:00
Bill Currie
071e529b0e [qfcc] Implement parsing side of ... macros
Expansion is not yet implemented (need __VA_OPT__ and __VA_ARGS__), but
this gets scheme compiling.
2023-10-30 13:28:01 +09:00
Bill Currie
eab561f279 [qfcc] Handle * strings in /* */ comments correctly
The *s were consuming the final star in */, so fancy comments were
breaking.
2023-10-30 13:22:57 +09:00
Bill Currie
61948b2469 [qfcc] Rework function-type macro expansion
It turns out that the recursive lexing was over-complicated as the
tokens for nested macros need to come from the expanded stream, not the
raw input stream.
2023-10-30 00:41:06 +09:00
Bill Currie
243a3de629 [qfcc] Grab undef id early
I'm unsure of the small string handling is a premature optimization, but
when most tokens are small...
2023-10-29 18:23:39 +09:00
Bill Currie
e402d0fa21 [qfcc] Implement command-line macro definitions
This takes care of __QFCC__ not being defined
2023-10-29 18:19:03 +09:00
Bill Currie
dfd57ed74f [qfcc] Generate dependencies only if not using cpp
cpp does this for us so the double-generation is redundant (and
currently wrong anyway with things like <built-in> and <command-line>
getting into the list of dependencies).
2023-10-29 14:00:37 +09:00
Bill Currie
6fa1d9caa2 [qfcc] Handle q-string in preprocessor expressions
This fixes line directives not parsing as they were getting c-string but
the preprocessor parser wasn't expecting them (and I suspect it
shouldn't).
2023-10-29 13:59:17 +09:00
Bill Currie
0990401a87 [qfcc] Get vkgen compiling
Or at least mostly so. The __QFCC__ define isn't visible, and it seems
undef might not be working properly (ruamoko/lib/types.r doesn't
compile). Of course, there's still the issue of whether it's compiling
correctly.
2023-10-28 23:51:12 +09:00
Bill Currie
c6ab5cd0ec [qfcc] Use generated tag for reporting errors
Avoids a segfault when the supplied tag is null (often the case when
qfcc defines the struct). Avoids a segfault when other errors cause
problems.
2023-10-28 08:45:27 +09:00
Bill Currie
b52b803c2a [qfcc] Add a #notice directive
Haven't got things working properly and I'm already extending the
preprocessor :P. It proved handy for debugging :)
2023-10-28 08:43:17 +09:00
Bill Currie
80c9132199 [qfcc] Treat all identifiers as undefined
If ID gets to the preprocessor parser in expressions, the ID is not
defined because if it was defined, it would have been expanded. Thus,
all IDs are 0.
2023-10-27 17:27:52 +09:00
Bill Currie
4214b90de3 [qfcc] Handle macros in preprocessor directors
I had forgotten that ids in preprocessor directives had a different
token type.
2023-10-27 17:27:02 +09:00
Bill Currie
733d242552 [qfcc] Don't try to define macros in suppressed code
Doing so kind of defeats the purpose of suppressing code.
2023-10-27 17:26:00 +09:00
Bill Currie
3bd391d01f [qfcc] Handle h-string and q-string only in #include
And #embed, though that's not implemented yet. Comparisons eating
multiple lines results in some rather interesting errors.
2023-10-27 17:24:54 +09:00
Bill Currie
823a9bd8d9 [qfcc] Implement dependency tracking generation
And now an issue with the vulkan headers shows up. Still, progress :)
2023-10-26 22:20:19 +09:00
Bill Currie
ae3a6c8b90 [qfcc] Move the dependency option handling to cpp.c
This cleans up options.c a little more and prepares for implementing
dependency tracking with the built-in preprocessor.
2023-10-26 21:21:00 +09:00
Bill Currie
92832a3b2c [qfcc] Improve handling of suppressed and continued lines
Now vkalias.r compiles with the builtin preprocessor. However, the build
fails due to the automatic dependency files not being generated.
2023-10-26 20:31:54 +09:00
Bill Currie
47c60f2bbf [qfcc] Handle quote includes better
The path for the current file wasn't being set correctly at startup and
"error: success" never makes much sense.
2023-10-26 20:28:43 +09:00
Bill Currie
ffb2514e75 [qfcc] Implement #include and search paths
Other than some trouble with line comments and continuation lines, it
seems to work nicely.
2023-10-25 21:07:50 +09:00
Bill Currie
f1f0a4a260 [qfcc] Update line directive handling
In addition to cleaning up the old flex line rules, this improves
handling of the '# num "file" flags' from cpp to at least parse the
additional flags (support for the system header flag might come later,
but I doubt the extern-c flag will have much meaning).

QuakePascal has lost its line directive handling (no errors, but dead
rules) for now. Eventually the lexers will be merged.
2023-10-25 12:22:33 +09:00
Bill Currie
717be4a12d [qfcc] Update pragma handling for the preprocessor
This removes the now redundant flex rules, though does spread
preprocess-only checks around a little more.
2023-10-25 09:25:00 +09:00
Bill Currie
31ee99ee0a [qfcc] Record identifiers in macros as rua_id
This ensures they always get recognized as identifiers in later stages.
2023-10-25 01:09:36 +09:00
Bill Currie
9dda656e1b [qfcc] Don't create macro params struct for object macros
Fixes object macros not expanding.
2023-10-25 00:52:36 +09:00
Bill Currie
4e922008eb [qfcc] Collect all arg tokens for stringification
Now all the tokens in the arg go into the one string.
2023-10-25 00:25:41 +09:00
Bill Currie
dc598c58b5 [qfcc] Record and expand macro arguments
Stringizing doesn't work correctly (stringizes only the first token, not
the whole argument), but things seems to work otherwise.
2023-10-24 23:32:13 +09:00
Bill Currie
9b717c2e1e [qfcc] Strip leading white space from message text
This fixes the missing text in #warning and #error messages.
2023-10-24 22:18:23 +09:00
Bill Currie
e60f538664 [qfcc] Implement function-type macro argument parsing
The arguments are not recorded yet, but argument parsing can consume the
whole file if the closing ) is missing (same as gcc).
2023-10-24 22:06:01 +09:00
Bill Currie
4b36817592 [qfcc] Silence report function for preprocessor-only
The preprocessor doesn't know what a function is and the main parser
isn't run, so the errors are always "At top level".
2023-10-24 22:01:03 +09:00
Bill Currie
2849ed06cd [qfcc] Skip converting preprocessor in macros
This fixes a segfault when parsing code resulting from expanded macros
with numbers in them.
2023-10-24 21:51:57 +09:00
Bill Currie
dd17e45052 [qfcc] Use a prefix for qc tokens
I had wanted to do this earlier but shied away from the large edit. Now
it became more necessary (and will become even more necessary when I get
to the glsl front-end).
2023-10-24 19:50:31 +09:00
Bill Currie
3307ef77ec [qfcc] Implement object-type macro expansion
Really, function-type macros expand too, but incorrectly as the
parameters are not parsed and thus not expanded, but this gets the basic
handling implemented, including # and ## processing.
2023-10-24 15:58:37 +09:00
Bill Currie
45e61544bb [qfcc] Improve handling of spaces in macro definitions
Avoids space tokens on either side of ## and after #
2023-10-24 14:17:05 +09:00
Bill Currie
5590ea5b75 [qfcc] Delay conversion of preprocessor tokens
Converting ID and char constants too early resulted in poor handling of
keywords and spurious diagnostics about multi-byte character constants,
particularly with -E (preprocess-only)
2023-10-24 13:23:06 +09:00
Bill Currie
e71816f9c4 [qfcc] Implement preprocessor-only output
Currently only to stdout, but it makes debugging preprocessing much
easier.
2023-10-23 22:28:03 +09:00
Bill Currie
8341f48f9d [qfcc] Implement #undef
That was pleasantly easy :)
2023-10-23 19:00:37 +09:00
Bill Currie
5cab587207 [qfcc] Implement macro recording and defined()
So far, very consistent with gcc's cpp from my limited testing, though
some error handling may be a little different.
2023-10-23 19:00:30 +09:00
Bill Currie
5794c68bda [qfcc] Implement basic conditional compilation
Just #if, #else and #endif for now. However, much of the preprocessor
parsing working minus the semantics (expressions do work, though).
2023-10-23 00:10:25 +09:00
Bill Currie
df3ffaaf0f [qfcc] Speed up comment processing significantly
Somewhere between 1.5x and 2x as fast (for 17MB of comments).
2023-10-21 11:35:58 +09:00
Bill Currie
2694aa4f1f [qfcc] Use flex's state stack
This will make handling preprocessor directives much easier.
2023-10-21 10:12:41 +09:00
Bill Currie
76cf0bf008 [qfcc] Clean up the rules a little
Mostly white space, but a little more consistency in handling and remove
the use of REJECT (which does actually seem to make a difference, but it
could be just noise).
2023-10-21 07:22:05 +09:00
Bill Currie
67642e5119 [qfcc] Clean up string lexing for preprocessing 2023-10-21 01:15:40 +09:00
Bill Currie
d800eea1db [qfcc] Use preprocessor numbers for literals
As far as I can tell, the preprocessor numbers conform with C23 except
for a couple of extensions (both ' and _ work for digit separators, and
d/D work for explicit doubles (since qfcc current defaults to float
instead of double)). This massively cleaned up the numeric rules and
even took care of some UB in the vector parsing code (I'm not sure which
is more surprising: that I didn't see it at the time, or that it was
blindingly obvious now).
2023-10-20 21:42:47 +09:00
Bill Currie
b973c5a738 [qfcc] Support implicit typing for new_value_expr
This makes creating implicitly type vector literals much easier.
2023-10-20 20:51:19 +09:00
Bill Currie
93247163b3 [qfcc] Create a preprocessor token type
This will be used for unifying preprocessing and parsing, the idea being
that the tokens will be recorded for later expansion via macros, without
the need to retokenize.
2023-10-20 20:39:13 +09:00
Bill Currie
28478befa6 Adjust lex files to work with updated git flex
While my modified version is needed to actually avoid warnings (vs
upstream git flex), the files still work with debian's flex (with no
warnings). I needed to update (and fix) flex so the lexer line numbers
would be correct.
2023-10-19 23:10:27 +09:00
Bill Currie
76c92fada0 [qfcc] Clean up implicit int exprs
Hides the const-cast away where the expression is created. Just cleaner
in general.
2023-10-19 22:52:17 +09:00
Bill Currie
12e53737ba [qfcc] Switch the parsers and lexers to full reentrant
This should make it more feasible to implement a preprocessor, and
definitely cleans some things up a little bit.
2023-10-12 23:49:37 +09:00
Bill Currie
3f0e793747 [qfcc] Set up to use bison locations
The improved location tracking isn't used yet, but was fairly invasive
on the bison-flex api.

It also cleans up some of the ancient workarounds for bad flex cores.
2023-10-08 11:02:49 +09:00
Bill Currie
a42f0a6bf4 [qfcc] Handle continuation lines in the lexer
It turns out I need to create my own cpp in order to handle glsl's
directives. I've decided to make a unified lexer, and continuation lines
seemed a good place to start.
2023-10-08 01:50:06 +09:00
Bill Currie
ba35ce71b3 [qfcc] Support 32-bit and 64-bit handle types
32-bit is nice because it's small, but 64-bit is handy for special
handle encodings.
2023-10-02 23:33:37 +09:00
Bill Currie
360dfc0038 [qfcc] Respect commutativity when optimizing adds
This fixes the really odd bug of certain string values getting swapped
in vkgen when DEBUG_QF_MEMORY was defined in expr.c. It will also
prevent a lot of fun with floats in the future, I imagine.
2023-10-02 22:33:29 +09:00
Bill Currie
0974983a39 [qfcc] Set expression associativity field
I forgot this when adding the option.
2023-10-02 22:32:23 +09:00
Bill Currie
7f42677a34 [qfcc] Clean up the last general uses of expr's next
It's now meant only for ALLOC. Interestingly, when DEBUG_QF_MEMORY is
defined in expr.c, something breaks badly with vkgen (no sniffles out of
valgrind, though), but everything is fine with it not defined. It seems
there may be some unpleasant UB going on somewhere.
2023-10-02 21:38:16 +09:00
Bill Currie
ef807f411e [qfcc] Check alignment when looking for immediates
I'm not sure why this showed up now (I guess just not enough large
immediate values), but this fixes a segfault in the algtypes test (the
mystery is why it showed up this late).
2023-10-02 21:14:14 +09:00
Bill Currie
4952d298ac [qfcc] Fix some memory errors
Found thanks to running valgrind over qfcc through the entire build
whith -DDEBUG_QF_MEMORY.
2023-10-02 21:14:14 +09:00
Bill Currie
471f5488ff [qfcc] Catch some error expressions
Not really enough in general as an internal error still occurs, but the
triggering error is caused by some broken changes.
2023-10-02 14:46:42 +09:00
Bill Currie
261ea0c4de [qfcc] Be more const-correct with expressions
Diagnostics that return an expression now return const, and fixes error
not returning an error expression.
2023-10-02 14:46:39 +09:00
Bill Currie
f3ca0c9222 [qfcc] Dump conflicting type expressions in debug 2023-10-02 12:06:14 +09:00
Bill Currie
b89e243c47 [qfcc] Improve handling of different types in ?: 2023-10-02 09:07:37 +09:00
Bill Currie
2134c85a47 [qfcc] Collect common multiplication terms
This gets my `m * p * ~m` code as optimal as possible if my counting is
correct (this does not include the extra extends and add needed to merge
the values). Also, there might be a possibility of recombining some ops
into a vector op, but I'm happy with this.
2023-10-01 23:17:51 +09:00
Bill Currie
f3edc06c45 [qfcc] Prefer constant scalar mults for collecting
This makes it more likely for a squared scale multiplier remaining
intact, resulting in slightly better code.
2023-10-01 22:31:35 +09:00
Bill Currie
04f49d1ca4 [qfcc] Commit common scale terms
While it works, and does improve the code slightly, it could do better
by favoring constants over variables for the common factor.
2023-10-01 21:53:50 +09:00
Bill Currie
e8521eb4cd [qfcc] Collect like terms into products
That is, `x+x -> 2*x` (and similar for higher counts). Doesn't make much
difference for just 2, but it will make collecting scales easier and I
remember some testing showing that `2*x` is faster than `x+x` for
floating point.

Of course, motor-point keeps bouncing around numerically :/
2023-10-01 19:01:36 +09:00
Bill Currie
96215ed749 [qfcc] Clean up some struct forward declarations
Having to have `struct foo` everywhere gets a bit annoying after a
while.
2023-10-01 17:45:27 +09:00
Bill Currie
ca1b455aa0 [qfcc] Collect common cross product terms
This reduces the number of cross products in `m * p * ~m` from 4 or 5 (4
after the old CSE went through the code) to 2 even before CSE.
2023-10-01 17:32:10 +09:00
Bill Currie
afe6ea526b [qfcc] Give mono-group multivec types structs
This fixes the motor-point.r test (ie, the sub-type field selector works
on mono-group types now). Still need to sort out something for scalars
(but I imagine that can work only in an @algebra context).
2023-09-30 16:33:14 +09:00
Bill Currie
87cf48ffc4 [qfcc] Sort factors in summed terms
This allows them to be matched with cancelling factors. My fancy zero
test is now just that: a fancy zero:

    typedef @algebra(float(3,0,1)) PGA;
    typedef PGA.group_mask(0xa) bivector_t;
    typedef PGA.group_mask(0x1e) motor_t;
    typedef PGA.tvec point_t;
    typedef PGA.vec  plane_t;

    plane_t
    apply_motor (motor_t m, point_t p)
    {
        return (m * p * ~m).vec;
    }

    0000 nop there were plums...
    0001 adjstk 0, 0
    apply_motor:
    motor.r:32:{
    0002 with 2, 0, 1
    motor.r:33:     return (m * p * ~m).vec;
    0003 return (<void>)

The motor-point.r test fails because it uses (m * p * ~m).tvec to get
the value but the type system is slightly broken in that a mono-group
algebra type does not have a structure associated with it and thus the
"missing" field results in 0. Yes, I spent too long chasing that one,
too.
2023-09-30 15:13:14 +09:00
Bill Currie
546253cea7 [qfcc] Add support for associativity
With (not yet hooked up) options for floating point.
2023-09-30 11:06:06 +09:00
Bill Currie
3f40977ecb [qfcc] Handle cancellations in large chains
It turned out I wasn't shuffling canceled terms correctly, thus far too
many hours of bug hunting.
2023-09-29 21:53:56 +09:00
Bill Currie
35ec9062cf [qfcc] Find the dagged expression for scales
I'd missed this in the previous commit, which was a good thing, really,
as it turns out this was the trigger of the bug that causes my fancy
zero test to become non-zero. It seems the bug is in either
component_sum or in the extend merging.
2023-09-29 18:39:33 +09:00
Bill Currie
f1f87527aa [qfcc] Move nested scale handling to distribute_product
Or really, the implementers. This gets my fancy zero test down to just
unrecognized permutations of commutative multiplies and dot products
(with the multiplies above the dot products).
2023-09-29 15:06:07 +09:00
Bill Currie
55ee8562df [qfcc] Use an implementation function for products
Another step towards proper handling of nested scales.
2023-09-29 13:37:41 +09:00
Bill Currie
355d3d76b4 [qfcc] Clean up nested scale handling
This is incomplete in that the handling needs to be moved into
distribute_product, but the infrastructure is there without breaking
anything.
2023-09-29 13:37:41 +09:00
Bill Currie
a21c857579 [qfcc] Correct error messages in pga3d test 2023-09-29 12:50:36 +09:00
Bill Currie
f5ba761c75 [qfcc] Clean up a pile of redundant code
The code was made redundant by distribute_product doing the right thing
for products that can go to 0.
2023-09-29 11:13:49 +09:00
Bill Currie
26dec9ee21 [qfcc] Move product negation into distribute_product
Now all the products are handled consistently, and distribute_product
takes advantage of anti-commutativity.
2023-09-29 11:08:53 +09:00
Bill Currie
14d1148523 [qfcc] Distribute products over sums
While this does "explode" the instruction count (I'll have to collect
like terms later), it does allow for many more opportunities for things
to cancel out to 0 (once (pseudo)commutativity is taken care of).
2023-09-29 10:16:00 +09:00
Bill Currie
2bf855657c [qfcc] Convert dot of scales to scaled dot
That is, dot(scale(A,a),scale(B,b)) -> (a*b)*dot(A,B). Does the right
thing when only one side is a scale. No change to the instruction count
in my fancy zero, but it does open more opportunities when I distribute
products.
2023-09-29 10:16:00 +09:00
Bill Currie
4bf6ce45d4 [qfcc] Convert chained scales to scale by product
That is, scale(scale(A,b),c) becomes scale(A,b*c), thus giving the
expression dag more opportunities to find common sub-expressions. My
fancy zero test is down to 20 total instructions (including overhead, or
16 for the actual algebra).
2023-09-29 10:16:00 +09:00
Bill Currie
bc63f211bb [qfcc] Split up pga3 4-component scales
While splitting up the scaled vector into scaled xyz and scaled w does
cost an extra instruction, it allows for other optimizations to be
applied. For one, extends get all the way to the top now, and there are
at most two (in my test cases), thus either break-even or even a slight
reduction in instruction count. However, in the initial implementation,
I forgot to do the actual scaling, and 12 instructions were removed from
my fancy zero case, but real tests failed :P It looks like it's just
distributivity and commutativity holding things back (eg,
omega*gamma*sigma - gamma*omega*sigma: should be 0, but not recognized
as that).
2023-09-29 10:16:00 +09:00
Bill Currie
8bdeead37f [qfcc] Clean up some whitespace 2023-09-29 10:16:00 +09:00
Bill Currie
83e4b2b7f5 [qfcc] Merge extend expressions in sums
This removes another 3 instructions from the fancy zero test.
2023-09-29 10:16:00 +09:00
Bill Currie
026533d56b [qfcc] Use ex_list_t for multivec components
This fixes the motor test :) It turns out that every lead I had
previously was due to the disabling of that feature "breaking" dags
(such that expressions wouldn't be found) and it was the dagged
multi-vector components getting linked by expr->next that made a mess of
things.
2023-09-29 10:16:00 +09:00
Bill Currie
cf4916e4de [qfcc] Make expression lists more generally usable
That much conflation was a bit excessive.
2023-09-29 10:16:00 +09:00
Bill Currie
210a925be4 [qfcc] Make expressions const-correct
Or at least mostly so (there are a few casts). This doesn't fix the
motor bug, but I've wanted to do this for over twenty years and at least
I know what's not causing the bug. However, disabling fold_constants in
expr_algebra.c does "fix" things, so it's still a good place to look.
2023-09-29 10:15:59 +09:00
Bill Currie
153a19937f [qfcc] Simplify algebraic sums
This doesn't fix the motor bug, but it doesn't make it worse. However,
it does simplify the trees quite a bit, so it should be easier to debug.
It seems the problem has something to do with fold_constants messing up
dagged aliases: in particular, const-folding multiplication by e0123 in
3d PGA as fold_constants sees it as 1.
2023-09-29 10:15:04 +09:00
Bill Currie
198cec6df8 [qfcc] Add a failing test case for dags
I'm not yet sure what went wrong, but the introduction of dags broke
something in my set_transform function (perhaps the dual?), but it's
something to do with the symbol being dagged (I guess because it's
required for everything else to dag). However, the strangest thing is
the error shows up with 155a8cbcda which
is before dags had any direct effect on the geometric algebra code. I
have a sneaking suspicion it's yet another convert_name issue.
2023-09-29 10:15:04 +09:00
Bill Currie
9c258b02e8 [qfcc] Be more cautious with summed extents
They should be treated as such only when merging vector components. This
fixes a bug that doesn't actually exist (it's in experimental code),
where the sum of two 3-component vectors was getting lost.
2023-09-29 10:15:04 +09:00
Bill Currie
c01cbf4fc4 [qfcc] Use distributivity to cancel cross and dot products
For cross products: remove any a from a×(...+/-a...)
For dot products: remove any a×b from a•(...+/-a×b...) (or b×a)

This removed another 2 instructions :)
2023-09-29 10:14:56 +09:00
Bill Currie
7271d2d570 [qfcc] Add flags for commutative and anticommutative
They don't have much effect that I've noticed, but the expression dags
code does check for commutative expressions. The algebra code uses the
anticommutative flag for cross, wedge and subtract (unconditional at
this stage). Integer ops that are commutative are always commutative (or
anticommutative). Floating point ops can be controlled (default to non),
but no way to set the options currently.
2023-09-25 17:26:37 +09:00
Bill Currie
5119237c4c [qfcc] Apply a×a=0
This takes advantage of the expression dag to detect when an expression
is on both sides of a cross product (which always results in 0). This
removes 3 instructions from my motor test (28 to go).
2023-09-25 16:57:15 +09:00
Bill Currie
6d2e3c166d [qfcc] Add GA algebraic expressions to the dags
Again, doesn't affect generated code yet, but it does make it easier to
work with the resulting graphs looking for cancellations.
2023-09-25 16:57:15 +09:00
Bill Currie
155a8cbcda [qfcc] Use dags for many expressions
Especially binary expressions. That expressions can now be reused is
what caused the need to make expression lists non-invasive: the reuse
resulted in loops in the lists. This doesn't directly affect code
generation at this stage but it will help with optimizing algebraic
expressions.

The dags are per sequence point (as per my reading of the C spec).
2023-09-25 16:57:15 +09:00
Bill Currie
d7714eeca7 [qfcc] Disable VM evaluation for non-ruamoko targets
This is really a bandaid for the problem of ruamoko not being an actual
superset of v6 progs (eg, no float | operation). With this, even ctf
builds.
2023-09-25 16:57:15 +09:00
Bill Currie
1e7b1ec86f [qfcc] Fix vector component symbols
The removal of the e tag from expr_t necessitated making convert_name
return a new expressions which resulted in get_type no longer being
enough to both convert a name expression and get the type. This was just
another missed spot. With this, all of game-source except ctf builds.
2023-09-25 16:57:15 +09:00
Bill Currie
79dbc9b866 [qfcc] Add pragma control of code options 2023-09-25 16:57:15 +09:00
Bill Currie
f974192177 [qfcc] Use non-invasive lists for function arguments
This allows expressions to be repeated (by reference) in function
argument lists, which will allow for expression dags.
2023-09-25 16:57:15 +09:00
Bill Currie
81b544c362 [qfcc] Use non-invasive lists for most expressions
This covers attribute params, vector, state, and comma expressions. Just
function args to go, I think.
2023-09-25 16:57:15 +09:00
Bill Currie
cc67e69923 [qfcc] Use non-invasive lists for block expressions
They will be used for other expression types too. Invasive lists make it
difficult to do expression dags.
2023-09-25 16:57:15 +09:00
Bill Currie
345eba45d5 [qfcc] Make the expression union anonymous
Finally, that little e. is cleaned up. convert_name was a bit of a pain
(in that it relied on modifying the expression rather than returning a
new one, or more that such behavior was relied on).
2023-09-23 18:01:49 +09:00
Bill Currie
9af94da151 [qfcc] Rename ex_list_t to ex_boollist_t
I need to create an actual expression list type and want the name.
2023-09-22 20:26:41 +09:00
Bill Currie
1183d44361 [qfcc] Skip dag dependency lines for leaf nodes
Leaf nodes never generate code so showing lines to them only cluttered
the displayed dag (they're still there internally, though).
2023-09-19 11:47:28 +09:00
Bill Currie
1e274b385d [qfcc] Improve handling of negative GA expressions
Sum expressions pull the negation through extend expressions allowing
them to switch to subtraction when appropriate, and offset_cast reaches
past negation to check for extend expressions. This has eliminated all
negation-only instructions from the motor-point, shaving off more
instructions (now 27 not including the return).
2023-09-19 00:47:29 +09:00
Bill Currie
ca01ab2a27 [qfcc] Improve offset_cast's zero detection
I'd caught 0 single components, but I'd missed 0 vector components. This
shaves 4 instructions off the motor-point test.
2023-09-18 21:43:23 +09:00
Bill Currie
068f685fde [qfcc] Simplify offset_cast for correctness
I don't know why I didn't think to do it this way before, but simply
recursing into each operand for + or - expressions makes it much easier
to generate correct code. Fixes the motor-point test.
2023-09-18 19:36:15 +09:00
Bill Currie
7646f6b51e [qfcc] Add failing test case for motor-point sandwich
This tests a subtle bug where part of the product is dropped on the
floor due to an issue with aliasing through expand expressions.
2023-09-18 19:34:22 +09:00
Bill Currie
ea375a4e2f [qfcc] Fix @dot for vecN types
The change from @dot producing a vector to producing a scalar missed
basic vecN types.
2023-09-14 21:29:59 +09:00
Bill Currie
e535fd51b7 [qfcc] Make passing int constants through ... ok
That is, passing int constants through ... in Ruamoko progs is no longer
a warning (still is for v6p and v6 progs). I got tired of getting the
warning for sizeof expressions when int through ... hasn't been a
problem for even most v6p progs, and was intended to not be a problem
for Ruamoko progs.
2023-09-14 18:33:06 +09:00
Bill Currie
862b2669a5 [qfcc] Ensure progs defs are sorted by address
This ensures they can be found by the progs debug support (since they do
a simple bsearch).
2023-09-12 22:26:30 +09:00
Bill Currie
3f93f6b226 [qfprogs] Create a stack for disassembly of locals
This prevents local variables getting intertwined with globals when
disassembling.
2023-09-12 22:21:55 +09:00
Bill Currie
8ec66f118a [qfcc] Set a flowvar's minimum size based on usage
But really only for memset and memmove because they need to use an int
alias of the variable and it may be only that alias that sets a much
larger variable.
2023-09-11 22:59:40 +09:00
Bill Currie
73d1044bec [qfcc] Support offset aliases of values
But explicitly not for pointers (even an offset of 0 breaks the pointer
relocation).
2023-09-11 22:56:10 +09:00
Bill Currie
b456f936c6 [qfcc] Fix vector expressions for generic vectors
It seems the code was never really tested due to an incorrect progs
version test.
2023-09-11 12:31:08 +09:00
Bill Currie
91c3049348 [qfcc] Ensure scale expressions have the correct type
fold_constants was replacing the pseudoscalar type with a float. I
really need to rewrite that whole block sometime.
2023-09-11 12:30:38 +09:00
Bill Currie
eb9954700a [qfcc] Simplify algebra_field_expr
This removes all the special cases and thus it should be more robust. It
did show up some out-by-one (or a factor of two!) errors elsewhere in
the group mask calculations.
2023-09-11 10:25:35 +09:00
Bill Currie
c066e638e5 [qfcc] Take memset/move size into account for use/def
This fixed the bogus uninitialized variable working in b648c353f
2023-09-11 00:37:53 +09:00
Bill Currie
b648c353ff [qfcc] Add a failing test for bogus uninitialized
The variable is very definitely initialized... by a memset (which is the
problem, actually).
2023-09-11 00:34:15 +09:00
Bill Currie
4bcbfc3473 [qfcc] Fix an uninitialized test
When the function is empty, not even a stack adjust instruction is
emitted, so checking for one wound up accessing uninitialized memory.
2023-09-11 00:32:01 +09:00
Bill Currie
2d7b5c18af [qfcc] Add named types to algebra subtypes
I'm uncertain about the names, but this makes it much easier to get at
specific subtypes (eg, PGA.tvec as a type) rather than having to know
the group mask.
2023-09-11 00:31:27 +09:00
Bill Currie
d387b56f60 [qfcc] Support block initializers for multi-vectors
They're not supported (yet) for single-group multi-vectors, and
designators are required for all initializer elements.
2023-09-11 00:24:50 +09:00
Bill Currie
b35f4c82a4 [qfcc] Implement field access for multi-vector expressions
Of course as an r-value (I think), but it makes getting only the desired
part of a geometric algebra expression much nicer.
2023-09-10 13:13:49 +09:00