Commit graph

3728 commits

Author SHA1 Message Date
Bill Currie
d46620fcf4 [qfcc] Add declaration expressions
Now declarations can be deferred too, thus things like generic/template
and inline functions should be possible. However, the most important
thing is this is a step towards a cleaner middle layer for compilation,
separating front-end language from back-end code-gen.
2024-10-04 11:51:34 +09:00
Bill Currie
8b9333e108 [qfcc] Remove union hack for block expressions
I don't remember why I thought it was a good idea at the time, but I
decided that having the union was a bit iffy and making the list
"official" would be a good idea. In the end, it removed a nice chunk of
code (redundant list manipulations).
2024-10-03 19:11:49 +09:00
Bill Currie
dcd2505bb9 [qfcc] Align cast operands
I'm surprised alignment didn't bite me earlier. Fixes a surprise
segfault in vecconst.r.
2024-10-03 19:11:49 +09:00
Bill Currie
8853d56af3 [qfcc] Ensure only one type def for functions
Only aggregate types are allowed to have duplicate type definitions. It
seems I forgot to do the id check for function types.
2024-10-02 02:12:07 +09:00
Bill Currie
56f8e19f63 [qfcc] Implement specialization constants
And they even pass validation (though it turns out there's a bug in
glslangValidator regarding specialization constants with expressions (or
possibly spirv-val, but it seems to be the former as my bug report shows
signs of activity in that direction)).

https://github.com/KhronosGroup/glslang/issues/3748
2024-10-02 02:11:53 +09:00
Bill Currie
a7639a685e [qfcc] Add field and array expression types
And implement enough AST processing to compile fstrianglest.vert

Plenty of code-gen issues, though.
2024-10-02 02:01:33 +09:00
Bill Currie
feca9bd5cb [qfcc] Rename new_field_expr to new_deffield_expr
I want to use new_field_expr for specialized field expressions instead.
However, I don't particularly like new_deffield_expr as a name, but I
can't think of anything better just yet.
2024-10-02 01:52:34 +09:00
Bill Currie
5a9ef35199 [qfcc] Add constexpr initializers to the ctor list
So far, this affects only glsl (because only glsl marks expressions as
constexpr), but it does get specialization constant initializers for
global variables working.
2024-10-01 17:10:00 +09:00
Bill Currie
f73e1bf353 [qfcc] Move ctor creation out of class_finish_module
Now, ctor expressions are collected and emitted after all other code,
and the ctor function being created outside of class_finish_module means
it's no longer limited to just class related initialization.
2024-10-01 16:51:49 +09:00
Bill Currie
3af078628e [qfcc] Preserve symbol expressions when constexpr
This prevents them from getting folded out of existence and thus
impossible to specialize.
2024-10-01 14:42:30 +09:00
Bill Currie
491b612888 [qfcc] Avoid (null) labels for dot graphs
I got tired of seeing it when looking at debug dumps of expressions.
2024-10-01 14:30:09 +09:00
Bill Currie
fc4e8cb350 [qfcc] Switch to deferred expressions for glsl
I plan to do this eventually for Ruamoko, but I need it to keep working
for now; it's rather nice having multiple languages. I expect this will
open up a lot of options for inlining, generic/template function
instantiation, etc. Right now, it's helping with specialization
constants in glsl.
2024-10-01 14:30:09 +09:00
Bill Currie
a97fc8154a [qfcc] Set all the commutativity flags
Including anti-commutativity. I hadn't set them before because I was
uncertain and got conflicting answers, but when I took a look again, it
seems IEEE 754 does guarantee commutativity for multiplication and
addition, so long as NaN isn't involved (and then it seems to be because
`a==a` is always false when a is a NaN).
2024-09-30 19:23:37 +09:00
Bill Currie
eade7e9a85 [qfcc] Add anti-commutative flags for cross and fp sub
They're still not set, but the commutativity can be adjusted
individually.
2024-09-30 19:23:37 +09:00
Bill Currie
275201afdf [qfcc] Optimize negating anti-commutative operations
Both for geometric algebra and in general (since it's just in
unary_expr). However, it doesn't quite work for float stuff (eg,
`vector × vector`, but that's only because the anti-commutative flag
isn't set.
2024-09-30 19:23:37 +09:00
Bill Currie
9797dcb5de [qfcc] Optimize (a+a)/2 in geometric algebra expressions
Really, I need to look into getting the optimizations more general, but
`a × ⋆a` is now just a single cross product instead off cross, add and
divide.
2024-09-30 19:23:37 +09:00
Bill Currie
b00d4d3b5b [qfcc] Respect parentheses when scattering sums
It may not be ideal at all times, but it does conform better with
general requirements for floating-point math.
2024-09-30 13:51:52 +09:00
Bill Currie
91febd50c6 [win] Fix some bitrot for building
Needed an option to disable LTO, and a couple of errors that crept in
with preprocessor and vulkan improvements.
2024-09-24 19:49:41 +09:00
Bill Currie
f4d8af400e [qfcc] Apply suspenders and a belt to the dimension check
Maybe there's a different trick to making gcc see that the dimension was
already check, but this works nicely size if the two calculations
differ, there are bigger problems.
2024-09-24 11:17:01 +09:00
Bill Currie
594ba86aae [qfcc] Start emitting spir-v expression instructions
They're currently wrong since they're meant to be for specialization
constants (and that whole system is currently broken anyway) but are
instead raw code expressions, but progress is progress.
2024-09-23 18:56:18 +09:00
Bill Currie
5c9ba80d3a [qfcc] Rewrite unary_expr to be more maintainable
Result type and constant handling is now table-driven, resulting in the
removal of seven switch statements (and thus a lot less hassle when
extending types or expressions). Also, (u)long and (u)short are fully
implemented.

In addition, other than result type handing for boolean results, any
back-end specific implementation is now in the back-end.
2024-09-22 16:21:21 +09:00
Bill Currie
f5d3507819 [qfcc] Split out unary_expr to its own file
The plan is to clean up unary_expr like binary_expr.
2024-09-22 12:44:12 +09:00
Bill Currie
8099c4cf8e [qfcc] Support the idea of constexpr
It's nowhere near complete, but unary and binary expressions that are
marked as constant will not be subject to constant folding. This is
necessary for proper support of specialization constants.
2024-09-22 12:28:37 +09:00
Bill Currie
e7710bb7ac [qfcc] Give symtabs a name field
It's currently just to help with debugging now that I've got more
less-random symtabs floating around.
2024-09-21 23:09:38 +09:00
Bill Currie
6e4812f914 [qfcc] Mark symbols as being lvalue or not
This simplifies things a lot and fstrianglest.vert compiles again
(albeit incorrectly).
2024-09-21 22:08:51 +09:00
Bill Currie
753e21f56a [qfcc] Add inout expr dot output 2024-09-21 17:18:38 +09:00
Bill Currie
2c445380b1 [qfcc] Emit extensions and extinst imports
The imports need their result id recorded somewhere (and the hard-coding
in qc-parse.y removed), but that's a little bit of progress getting
spir-v working.
2024-09-20 14:03:03 +09:00
Bill Currie
e09f97685f [qfcc] Avoid ubsan related warning with lto
I'm not sure what's up (I doubt it's actually ub, it's just that it took
enabling ubsan *and* link-time-optimizations to see the warning), and
the warning is technically correct, though going from -1 (int) to
0xfffffff400000000 has me stumped, but ensuring width is 2-4 does take
care of it. All because I got a weird test failure that I wound up being
unable to reproduce after rebuilding qfcc (thus the ubsan pass, but no
other sniffles).
2024-09-20 13:30:28 +09:00
Bill Currie
186a781121 [qfcc] Allow single-constant attribute values
I don't remember why I though requiring a parameter list was a good
idea, but it makes working with attributes harder than it needs to be.
2024-09-20 12:28:02 +09:00
Bill Currie
1117d31b30 [qfcc] Make some sideways progress with layouts
I'm not sure it's the right directly, and qfcc's internals are starting
to show their age.
2024-09-20 12:24:51 +09:00
Bill Currie
34fc2108ea [qfcc] Use separate symbol types for def and offset
I don't know why I thought it was a good idea to make sy_var context
dependent. Renaming sy_var to sy_def makes it a little easier to know to
use the def field, too.
2024-09-20 11:39:16 +09:00
Bill Currie
6ca06a577a [qfcc] Make capabilities and memory model configurable
While I'm not happy with the module "creation" (at least it's limited to
two places), setting it up with spir-v capabilities and memory model
seems quite nice and should play nicely with being set up from within
source code, though using uint constant expressions might be overkill.
2024-09-20 01:39:44 +09:00
Bill Currie
e477148a9b [qfcc] Improve spir-v type emission
A few more types and function parameters are set up more or less
correctly (however, the pointer storage class is hard-coded for now:
need to add attributes to everything).
2024-09-18 15:12:57 +09:00
Bill Currie
7931eb8c03 [qfcc] Disable varargs for spirv
I'm not certain supporting varargs in spirv is impossible, but being
able to disable it might be worthwhile anyway.
2024-09-18 14:33:02 +09:00
Bill Currie
ee3a9bd200 [qfcc] Start work on emitting SPIR-V
It's rather non-functional but does so far pass validation via
spirv-val. However, it's showing that qfcc's internals need a lot of
work.
2024-09-17 16:47:19 +09:00
Bill Currie
d4c78a69f4 [qfcc] Fix uninitialized zero in component_compare
It was a bit of a surprise seeing a test case fail and then succeed
after making a minor edit to help find the bug, but valgrind to the
rescue. I'm surprised nothing showed up earlier.
2024-09-15 15:02:36 +09:00
Bill Currie
9cd8bc0d45 [qfcc] Move type encoding defs into an array
This allows the defs to be updated without having to modify the type
struct itself, and also allows for alternative encodings (eg, spir-v).
2024-09-15 12:05:57 +09:00
Bill Currie
c7e35994b5 [qfcc] Give type_t objects unique id indices
This allows indexing into arrays or sets of all types, good for various
mutable information.
2024-09-15 10:29:36 +09:00
Bill Currie
e933eb8fae [qfcc] Clean up expr parenthesis setting
Thare are still many const casts, but this does get rid of one set. I
had tried to replace the paren flag with a () unary expression, but that
brought out a whole pile of places that had problems (especially
anything to do with boolean expressions).
2024-09-14 08:31:26 +09:00
Bill Currie
4c603c3989 [qfcc] Remove rvalue flag from expr_t
It seems it was needed for dealing with the result expression for block
expressions, but it turns out (possibly thanks to dags), that it's easy
to check for the result value and using the appropriate call to emit the
code thus avoiding the non-executable code warning.
2024-09-13 21:41:40 +09:00
Bill Currie
cd051e8f9f [qfcc] Check handle types correctly
They can have a variety (min int and long) of backing types, so
requiring ev_invalid (which is used for struct and union).

Also, ensure is_int etc fail for handle types.
2024-09-12 14:10:44 +09:00
Bill Currie
355ee12c3f [qfcc] Give qfo spaces support for names
Fortunately, there was a reserved spot that could be used for the name
thus no need for a version update. The name isn't used yet but will be
for glsl support.
2024-09-12 13:50:33 +09:00
Bill Currie
99be6f6e41 [qfcc] Build namespaces for non-instanced blocks
Each interface type (in, out, uniform etc) gets its own namespace, and
non-instanced blocks get a namespace (their block name) within the
interface namespace.

The defs for the block members are currently "allocated" to be at offset
-1, but the idea is to allow layout qualifiers to know if the member has
already been located.
2024-09-12 13:33:42 +09:00
Bill Currie
1dc31f83af [qfcc] Implement namespace lookups
I don't know if they work correctly yet (because other areas are
incomplete), but they seem to do the job (at least no segfault).
2024-09-12 12:38:39 +09:00
Bill Currie
db39fdbe37 [qfcc] Create glsl block before parsing members
This allows attributes to be put on the block before members are
declared thus giving member attributes to override block attributes when
appropriate.
2024-09-12 12:14:10 +09:00
Bill Currie
f7f2790937 [qfcc] Use macro magic for the symbol type enum
I found that I'd missed an update or two of the names array.
2024-09-12 12:14:02 +09:00
Bill Currie
b1f4fc3131 [qfcc] Allow ~ on all int types.
I really need to revamp the unary operator code.
2024-09-11 17:33:13 +09:00
Bill Currie
224d1fd5f5 [qfcc] Fix misspelled compute shader name
And a gcc purity check.
2024-09-11 17:32:13 +09:00
Bill Currie
f0e770b972 [qfcc] Allow aligned despace allocs to be overridden
GLSL (and in the end, spir-v) needs more control over allocation than
does the Ruamoko VM.
2024-09-10 20:44:51 +09:00
Bill Currie
7742450e03 [qfcc] Do basic error checking on layout qualifiers
I'm not sure I got all the checks right, but bsp_gbuf.geom passes the
validity (but failes due to not having implemented the application of
the qualifiers).
2024-09-10 19:03:13 +09:00
Bill Currie
5d7a8127c2 [qfcc] Add is_handle type check function 2024-09-10 19:00:09 +09:00
Bill Currie
c58748d640 [qfcc] Implement many glsl qualifiers as attributes
Attributes seem appropriate as GLSL's qualifiers affect variables rather
than types (since there's no typedef).

Not much is done with the attributes yet other than some basic error
checking (duplicates of non-layout attributes) and debug output, but
most (if not all) declarations get to the declaration code with
attributes intact.
2024-09-09 20:01:06 +09:00
Bill Currie
ad3ae1abec [qfcc] Get true and false working in glsl 2024-09-09 11:47:01 +09:00
Bill Currie
2fc3a43e31 [glsl] Fix multiplication after addition
I'd goofed when transcribing the glsl grammar.
2024-09-09 11:42:30 +09:00
Bill Currie
7a2027276b [qfcc] Use a separate enum for glsl interfaces
While technically storage classes, they shouldn't pollute other
languages. It's a bit hacky mixing enums in one value, but having some
macros helps.
2024-09-09 11:40:57 +09:00
Bill Currie
c4c4b9f5b4 [qfcc] Make not a soft keyword
Its grammatical usage is such that it's unambiguous with identifiers,
and is needed for parsing the glsl functions (the not() function for
boolean vectors).
2024-09-07 02:42:32 +09:00
Bill Currie
13d38622f7 [qfcc] Copy parameter qualifiers when copying type
Fixes a segfault for some functions (generics?) cause by the new
parameter qualifiers.
2024-09-07 02:39:13 +09:00
Bill Currie
55a5782a95 [qfcc] Save the type evaluation code
I don't yet know whether the generated code is correct, but the little
functions that compute a generic type gets stored in the function's
params/return type.
2024-09-07 02:36:54 +09:00
Bill Currie
b59a8fb66a [qfcc] Add a code generator for type expressions
Allows the parsing of the return type in the following:

    @generic(vec=[vec2,vec3,vec4]) {
    @vector(bool,@width(vec)) lessThan(vec x, vec y);
    }

Unfortunately, can't use math in int value parameters just yet, the
processing of expressions needs to be delayed (it's currently done
immediately so type-checking happens to early).

It's not connected up yet, but does produce what looks like the correct
code.
2024-09-06 01:04:38 +09:00
Bill Currie
55d7d1f069 [qfcc] Expose convert_op and ensafen free_operand
Needed for type evaluation codegen, and free_operand needs to be
null-safe.
2024-09-06 01:04:38 +09:00
Bill Currie
9c902501d7 [qfcc] Move D_* macros to def.h
Finally, after way too long.
2024-09-06 00:56:56 +09:00
Bill Currie
1f3b21a626 [qfcc] Use a temp for size-changing swizzles
The Ruamoko instruction set has only same-size swizzles, so a staging
temp is needed either before (grow) or after (srink) the swizzle.
Unfortunately, it looks like there might be missed optimization issues,
but at least things seem to be correct.
2024-09-05 14:26:29 +09:00
Bill Currie
24a48ad703 [qfcc] Improve support for matrix types
Their size is now calculated correctly, they can be assigned,
initialized using block initializers (vectors too!), and columns can be
indexed as vector lvalues.
2024-09-05 14:06:30 +09:00
Bill Currie
698d27c996 [gamecode] Save all param pointers with PR_SaveParams
Builtins calling other functions that call back into progs can get their
parameter pointers messed up resulting in all sorts of errors. Thus wrap
all callbacks to progs in PR_SaveParams/PR_RestoreParams.

Also, ditch PR_RESET_PARAMS in favor of using PR_SetupParams and move
setting pr_argc into PR_SetupParams.
2024-09-05 00:00:44 +09:00
Bill Currie
f6d2cf43e8 [qfcc] Parse about 2/5 of the glsl functions
They're stuck on computed return types causing segfaults, but the rest
helped with ironing out other issues in the compiler.
2024-09-04 10:43:11 +09:00
Bill Currie
3aab301d45 [qfcc] Check receiver type a second type
The old "expr changed type to error" trick hasn't worked for a while, so
sending a message to an undefined class didn't end well.
2024-09-04 10:37:26 +09:00
Bill Currie
83b92dd067 [qfcc] Handle fixed return types in generic functions
More segfaults, wee.
2024-09-04 09:58:14 +09:00
Bill Currie
8f9785c3da [qfcc] Handle type function parameters in type functions
The code assumed an actual type reference, not a type function,
resulting in a segfault for `@vector(bool,@width(vec))`.
2024-09-04 09:54:29 +09:00
Bill Currie
cd7abf5320 [qfcc] Add bool and matrix types to ruamoko
They're not properly implemented, but they do at least parse, so they
can be used in declarations (just not expressions yet).
2024-09-04 09:50:00 +09:00
Bill Currie
64334759f4 [qfcc] Support multiple type specs in type_ref
This is needed for `unsigned int` or `unsigned long` in type
expressions. However, there is a problem where @vector etc complain
about just `unsigned` (which should default to int, of course).
2024-09-04 09:41:57 +09:00
Bill Currie
65fc6054bb [qfcc] Actually parse ruamoko from strings
The previous related commit was just the basics and only glsl used it,
now ruamoko itself does (really need to look into namings: qc is a bit
off).
2024-09-04 09:35:25 +09:00
Bill Currie
ebb558bef1 [qfcc] Abandon v6 and v6p inout tests
Getting them to work properly with automake proved to be a lot of
trouble, though the bug the v6 inout test was for actually had nothing
to do with inout.
2024-09-03 21:38:16 +09:00
Bill Currie
87f62d1d7c [qfcc] Split sblock after v6 and v6p function calls
I seem to remember being here before (but now I've got a comment in the
code). It seems .params and the dag code don't like each other. Fixes
the failing inout test for v6 progs. I suspect the only reason inout
worked for v6p was the use of rcall.
2024-09-03 20:52:00 +09:00
Bill Currie
0c24f436ae [qfcc] Add a failing test for v6 inout params
Getting the vector literal compiling proved to be interesting.
2024-09-03 20:50:31 +09:00
Bill Currie
291f920e2a [qfcc] Parse vector literals correctly
It turned out that for v6 progs (due to lack of double or long) weren't
getting correctly parsed vector literals: incorrect "implicit" flag and
then a lot of brittleness around constant value conversions.
2024-09-03 20:50:31 +09:00
Bill Currie
b58e7791fd [qfcc] Implement parameter qualifiers in Ruamoko
Now parameters can be declared `const`, `@in`, `@out`, `@inout`. `@in`
is redundant as it's the default, but I guess it's nice for
self-documenting code. `const` marks the parameter as read-only in the
function, `@out` and `@inout` allow the parameter to pass the value back
out (by copy), but `@out` does not initialize the parameter before
calling and returning without setting an `@out` parameter is an error
(but unfortunately, currently detected only when optimizing).

Unfortunately, it seems to have broken (only!) v6 progs when optimizing
as the second parameter gets optimized out.
2024-09-03 18:07:42 +09:00
Bill Currie
4dd461d1e6 [qfcc] Clean up make_qc_param
The code for it and make_param were nearly identical, but it turned out
that just setting up the symbol correctly was all that was needed before
passing the spec to make_param. This will make implementing parameter
qualifiers easier (less repetition).
2024-09-02 15:59:06 +09:00
Bill Currie
6f0c7eaba6 [qfcc] Fix segfault for mixed generic and static params 2024-09-01 21:25:42 +09:00
Bill Currie
0385345d63 [qfcc] Create defs for instanced interface blocks
And allow unsized arrays (for in, uniform and buffer) to have deferred
sizes.
2024-09-01 17:10:10 +09:00
Bill Currie
f0eccc398e [qfcc] Fix some glsl parsing issues
Segfault when debug printing expr values, missing spec_merge for
type_qualifier and superfluous ; after for loop init expression.
2024-09-01 17:02:42 +09:00
Bill Currie
f335540e99 [qfcc] Handle glsl version and extension directives
The version directive really does only some error checking, and
only GL_EXT_multiview and GL_GOOGLE_include_directive are supported for
extensions, but enable/disable work (but not yet warn for multiview).
2024-09-01 17:01:05 +09:00
Bill Currie
cba28e9421 [qfcc] Use correct flags for in and uniform
They're not actually constant (in the usual sense), just read-only.
Fixes an internal error when accessing an unsigned array.
2024-09-01 16:40:47 +09:00
Bill Currie
a24d1d799f [qfcc] Use a stack for cpp_quote_start
Using set_line_file sort of worked with its stack, but line directives
embedded in the source (which glsl's initialization code uses) messed up
the start path for quoted include searches.
2024-08-31 13:01:26 +09:00
Bill Currie
9b2b841a55 [qfcc] Redo interface blocks as namespaces
There's no direct support for namespaces in Ruamoko yet, nor even in
qfcc, but glsl's blocks bring in a bit of foundation for them, even the
concept of "using" (for blocks with no instance name).

The members don't get locations allocated to them yet, but
fstrianglest.vert compiles and links correctly otherwise.

Also, there's no error checking yet.
2024-08-31 10:32:00 +09:00
Bill Currie
291898b46f [qfcc] Properly dup symbol for declaration
The unification of qc and c function symbol handling made it important
that the new symbol was a proper duplicate (minus being in a table) of
the old symbol. This fixes redeclared prototypes (especially for
qc-style functions, not encountered for c-style). Complete with unit
test :)
2024-08-30 21:45:37 +09:00
Bill Currie
ee6e38e2f9 [qfcc] Implement glsl's contructors for math types
I don't think this includes bools yet, and matrices aren't supported
yet, but float vectors work quite nicely.
2024-08-30 13:54:33 +09:00
Bill Currie
d0064c6c46 [qfcc] Implement a lot of glsl semantics
Other than contructors (and problems with the `out` block) qfcc can
compile fstrianglest.vert to what looks like working ruamoko code.
There's still a lot of work to do, though.
2024-08-30 13:51:31 +09:00
Bill Currie
4b369be8c1 [qfcc] Report correct type in enum/struct redefinitions
Enums are neither unions or structs.
2024-08-30 13:47:36 +09:00
Bill Currie
6d9e2bd926 [qfcc] Allow a single void typed parameter
It makes dealing with `foo(void)` a little easier in glsl's parser.
2024-08-30 13:32:32 +09:00
Bill Currie
1510d21cde [qfcc] Implement is_matrix correctly
There was a FIXME from before I'd figured out how I wanted to deal with
them. Now is_matrix returns true only if there's at least 2 columns.
2024-08-30 13:17:43 +09:00
Bill Currie
bca4d0e794 [qfcc] Implement location printing for the parsers
It's pretty bare-bones, but it's at least consistent across all the
parsers, and it's currently used only by the parser debug support.
2024-08-30 13:13:13 +09:00
Bill Currie
daaaeb9aed [qfcc] Abort qfo loading if file is too small
Or really, if Qfilesize thinks it's too small. Trying to allocate -1
bytes (due to trying to open / as a qfo) didn't end well.
2024-08-29 01:04:52 +09:00
Bill Currie
b302ce1095 [qfcc] Hook up glsl builtin variables
They get parsed, but nothing gets defined yet, but this proved to be a
good test for rua_parse_string().
2024-08-28 13:53:30 +09:00
Bill Currie
de6a3c5702 [qfcc] Support parsing Ruamoko from strings
Handy for parsing builtin symbols for things like glsl.
2024-08-28 13:50:36 +09:00
Bill Currie
4be1384701 [qfcc] Improve language selection
Using a struct with function pointers instead of switching on an enum
makes it much easier to add languages and, more importantly,
sub-languages like glsl's shader stage variants.
2024-08-28 10:18:21 +09:00
Bill Currie
bbb90ce27a [qfcc] Clone metafunc for generated generic symbols
Simply referencing the original metafunc resulted in only the first
variant getting a def. Now my little test generates defs for all called
variants of a generic function.

However, I'm still not sure this is quite the direction I want to go
with making calls to generic functions, but I still need to figure out
defining them. I think making progress with the glsl front-end will
help.
2024-08-28 09:03:56 +09:00
Bill Currie
4f5fcbd819 [qfcc] Check all generic function variants for dups
Checking only the last function to be added results in false negatives
and thus duplicates when defining a generic function. eg:

    genFType radians (genFType degrees);
    genDType radians (genDType degrees);
    genFType radians (genFType degrees) = #0;
    genDType radians (genDType degrees) = #0;
2024-08-27 14:41:38 +09:00
Bill Currie
c236129bbe [qfcc] Parse generic functions early
Detecting generic functions needs to be done before finalizing the
function type for non-generic functions, otherwise the resulting
function type winds up being incorrect due to bogus resolution of the
return type (and probably a few other factors).
This takes care of handling the return type in function definitions as
well as declarations as
2024-08-27 14:37:18 +09:00
Bill Currie
27286389d1 [qfcc] Isolate functions from symbols
Symbols now use metafunc_t to reference functions, which should make
working with generic and overloaded functions easier.
2024-08-26 15:50:27 +09:00
Bill Currie
8eae02209e [qfcc] Unify qc and c function symbol handling
This gets all the function symbol type handling into the one place,
which will make dealing with generic functions much easier.
2024-08-26 12:58:39 +09:00
Bill Currie
80df79a62f [qfcc] Be more clear about the builtin id parameter
While `expr` is better than `$n`, `bi_val` seems even better at
conveying meaning.
2024-08-26 10:31:57 +09:00
Bill Currie
3abf69cb36 [qfcc] Report errors accessing source files
This got lost in the switch to internal preprocessing as cpp used to do
the job for us.
2024-08-21 16:12:52 +09:00
Bill Currie
6a70ecda9d [qfcc] Make default_type take const symbol_t
It doesn't need to modify sym, and being const makes it more clear that
it doesn't (despite some of the other type functions breaking that rule,
ugh).
2024-08-21 08:30:15 +09:00
Bill Currie
43f83fc0eb [qfcc] Clean up some redundancy in methodproto
I don't know why I didn't apply the same ideas as in methoddef, maybe I
just forgot. I'm pretty sure I did methodproto first, or maybe cleaned
up methoddef much later then didn't think of methodproto. Still, the
grammar is much nicer to read now.
2024-08-21 08:26:31 +09:00
Bill Currie
a00b17bf71 [qfcc] Check declarator type chains
Arrays of functions or functions that return arrays or functions aren't
valid. While working on how to get generics in properly, I finally
understood what's going on with function types in the specifier, and I
think I'll be able to sort out function pointers vs prototypes, too.
2024-08-21 08:23:12 +09:00
Bill Currie
856761c4a9 [qfcc] Add to a FIXME comment 2024-08-20 20:20:05 +09:00
Bill Currie
5e6832294e [qfcc] Handle parsing generic builtin functions
Generic qc-style prototypes don't work but both c and qc style
definitions get as far as trying to build the builtin function. It
starting to look like I need to rework function handling, which isn't
all that surprising as it hasn't changed much over 22 years.
2024-08-20 15:19:31 +09:00
Bill Currie
219f2f26da [qfcc] Add is_double_val check
I wound up not using it, but I'll probably want it eventually..
2024-08-20 15:16:25 +09:00
Bill Currie
b8fe10f8a5 [qfcc] Limit builtin numbers to ints
Or whole-number floats for v6 progs.
2024-08-20 15:13:57 +09:00
Bill Currie
7f021b074c [qfcc] Treat fp constants as float for v6 progs
v6 progs don't support doubles, so demote double suffices to float, and
treat implicit fp constants as float.
2024-08-20 14:49:24 +09:00
Bill Currie
d9e2108b91 [qfcc] Return a metafunc for generic functions
One step closer to getting this idea to work.
2024-08-20 12:43:17 +09:00
Bill Currie
67c380e98d [qfcc] Clean up function.h a little
And even remove a dead function.
2024-08-20 09:26:24 +09:00
Bill Currie
0aab95eefe [qfcc] Rename overloaded_function_t to metafunc_t
I never did like overloaded_function_t as a name, and with the
introduction of generic functions (or templates, I guess?) meta-function
makes more sense to me.
2024-08-19 18:29:44 +09:00
Bill Currie
34b79ce783 [qfcc] Clean up some unnecessary type const casts
Most of them were noise from the type const correctness pass, but the
qc field function handling was always dubious (though in practice safe
due to how the type was built, but...). The remaining casts outside of
type.c need some thought.
2024-08-19 13:04:19 +09:00
Bill Currie
cdf49bb6b7 [qfprogs] Use a backup type array for qcc progs
I was investigating how qcc dealt with field fields and got segfaults
from qfprogs due to the lack of type encodings. While the resulting
output is not guaranteed to be correct (especially if trying to dump
progs compiled by some other extended compiler), at least it's better
than segfaulting or any other UB.
2024-08-17 15:16:09 +09:00
Bill Currie
9eb6aba02c [qfcc] Make the immediate union anonymous
Yay, no more annoying tags that served no real purpose (though they did
make some things a little easier, there's generally a better way).
2024-08-16 17:30:02 +09:00
Bill Currie
561f4c2ea0 [qfcc] Make the type union anonymous
This one has bothered me for a long time. I don't remember why it was
difficult at one stage, maybe I had some poor name choices.
2024-08-16 17:23:29 +09:00
Bill Currie
e5bd2591c2 [qfcc] Make the value union anonymous
Another little tag bites the dust.
2024-08-16 17:12:26 +09:00
Bill Currie
ce8c72c323 [qfcc] Make the symbol union anonymous
And now that little s. goes away. Yay, one bit less noise when reading
code.
2024-08-16 16:48:11 +09:00
Bill Currie
6bd26a0f61 [qfcc] Add some TODO items
They came up when talking about math with Darian
2024-08-12 08:51:39 +09:00
Bill Currie
e13dce882a [qfcc] Produce working calls to generic functions
There's still the problem of defining implementations, but this gets a
lot of things working so long as the return type is one of the parameter
types rather than computed.
2024-08-11 22:40:21 +09:00
Bill Currie
2cfc7a342d [qfcc] Update doc for recent lyx
And document type expressions to a certain extent. A lot of work needs
to be done as there's a lot of bitrot in the information in the doc (eg,
integer is now int, array declarations, etc).
2024-08-11 20:46:55 +09:00
Bill Currie
79c4eb531e [qfcc] Implement generic function resolution
A working call isn't produced yet, but the generic function does seem to
be selected correctly preferring a minimum of type promotions, though I
suspect I'll need to do more work on the selection process.
2024-08-11 20:46:55 +09:00
Bill Currie
cd973c6809 [qfcc] Tighten up type_promotes
Mainly to prevent promotion between different vector and matrix sizes,
but also to be consistent in that double doesn't promote to itself (same
as other scalar types). Also fix up type_rows and type_cols for matrices
and make type_assignable, type_promotes, and type_compatible null-safe
(always false if either src or dst is null).
2024-08-11 20:46:55 +09:00
Bill Currie
d0a9e1f54f [qfcc] Clean up function search a little
Still some ways to go, but this will help with implementing generic
function calls.
2024-08-11 20:46:55 +09:00
Bill Currie
66866f79b6 [qfcc] Expand @vector and @matrix type expressions
With this, genFType and genDType functions are now treated separately
and expanding to all components (single row or column matrices are not
supported (at this stage, anyway) for generic parameters).
2024-08-11 20:46:55 +09:00
Bill Currie
58bf1ee64e [qfcc] Support generic scope blocks
That is, `@generic(...) { ... };`, which is handy for bulk declarations
(such as for glsl). This proved to be a lot harder than expected, I
suspect handling of specifiers needs a lot of work.
2024-08-11 20:46:55 +09:00
Bill Currie
0d063c3290 [qfcc] Clean up specifier creation
I never did like all those cryptic parameters, especially when they were
just 0 and 1. Now the individual specifier creation functions take only
those parameters they need (if any) and there's no possibility of
conflicting parameters. Also, it's clearer what the new specifier does.
2024-08-11 20:46:55 +09:00
Bill Currie
fa3c872970 [qfcc] Add some comments about bvect and bvecp
I really need to come up with something better, but for now, just having
the terms recorded somewhere helps.
2024-08-11 20:46:55 +09:00
Bill Currie
f1afa1caf0 [qfcc] Parse generic function declarations
It doesn't properly differentiate between (treats genDType as being the
same as genFType):

    @generic(genFType=@vector(float)) genFType radians(genFType degrees);
    @generic(genDType=@vector(double)) genDType radians(genDType degrees);

but this is due to problems with how the type is built from
@vector(float) and @vector(double). However, I thought it was about time
I got some of this into git.

Also, `@generic(...) { ... };` blocks don't work properly (they lose the
generic info): need to get a little smarter about handling generic scope
in `external_def_list`.
2024-08-11 20:46:55 +09:00
Bill Currie
4c424fcb61 [qfcc] Add a check for bool types
And us it in matrix_type. Although there are no bool matrices even in
glsl, there are bool vectors and vector_type uses matrix_type.
2024-08-10 14:22:20 +09:00
Bill Currie
8fe23a29b3 [qfcc] Clean up some double semicolons
None of them really affected anything, but I have seen them cause
problems in the past.
2024-08-10 14:19:07 +09:00
Bill Currie
80e493e588 [qfcc] Parse generic function declaration
The parsed generic function declaration is for ease of matching call
signatures with the appropriate implementation.
2024-07-07 21:42:52 +09:00
Bill Currie
f2609b1a9b [qfcc] Make save_string null-safe
It simply returns nullptr when the saved string is null.
2024-07-07 15:18:51 +09:00
Bill Currie
628e3d2aed [qfcc] Make function type return type more clear
No more guessing if that's the functions return type or something else.
2024-07-07 15:17:06 +09:00
Bill Currie
c681d3ee28 [qfcc] Tidy up the doc structure a little
Talking about variables in the types section didn't make much sense.
2024-07-04 23:53:04 +09:00
Bill Currie
97e0427565 [qfcc] Ensure type expressions are resolved
Type expressions should be usable anywhere regular types can be used.
2024-07-04 23:44:07 +09:00
Bill Currie
f11f0c5477 [qfcc] Return an error expression for undefined symbols
Fixes some of the error spam that resulted from the expression
const-correctness pass a while back.
2024-07-04 23:23:06 +09:00
Bill Currie
b3fed7282e [qfcc] Make @function a type function
This fits in nicely with the rest of the generic type system and makes
it a little more useful. The idea is it will take a return type (already
does since type functions always require a parameter at this stage) and
a parameter list (not implemented yet). It currently resolves to the
basic void (...) function type for QC.
2024-05-31 16:39:50 +09:00
Bill Currie
a655e6cef3 [qfcc] Deal with generic types in prototypes
Mostly avoiding segfaults or mistreating type expressions as the default
type.
2024-05-31 13:51:38 +09:00
Bill Currie
8da8bd9917 [qfcc] Pass full specifier to function_symbol
With generic types, is_overload is no longer enough as the type
expression needs to be parsed in order to create the symbol.
2024-05-31 13:44:52 +09:00
Bill Currie
38b1d2d238 [qfcc] Clean up some bool types
It's true that true is more meaningful than 1.
2024-05-15 00:22:12 +09:00
Bill Currie
e295a62050 [qfcc] Clean up function code a little
Replace struct forward declarations and some non-locally (to use)
declared locals.
2024-05-12 12:48:50 +09:00
Bill Currie
99caaaa010 [qfcc] Remove redundant parameter
It turns out function_symbol was never called without allowing creation
of the symbol, so no need for the parameter.
2024-05-12 12:24:50 +09:00
Bill Currie
f7720a4e44 [qfcc] Preserve current function across function blocks
I'm not sure if I want to support nested functions in Ruamoko, but
handling them without segfaulting is far more important.
2024-05-05 16:46:53 +09:00
Bill Currie
b935540d19 [qfcc] Remove qc special function symbol functions
It turns out they're not needed as the difficulty of quakec function
declarations (function fields) is taken care of by qc_function_spec.
Also, they made it difficult to think about function declarations.
2024-05-04 22:08:38 +09:00
Bill Currie
28bba12e87 [qfcc] Rename some functions
One a typo, the other for better consistency.
2024-05-04 10:50:08 +09:00
Bill Currie
1567f29668 [qfcc] Handle type expressions when merging specifiers
The type expressions no longer get lost along the way to the function
return type and parameter types.
2024-05-03 19:43:58 +09:00
Bill Currie
aff70aa243 [qfcc] Support static type expressions in declarations
This allows types in declarations to be based on other types:

    int foo[3];
    @vector(float,sizeof(foo)) bar;
2024-05-03 19:37:11 +09:00
Bill Currie
fed1bce12a [qfcc] Add a function to check for error expressions
I haven't gone through and replaced all the existing tests, but hiding
the details makes sense and fits with many of the other check functions.
2024-04-30 11:16:27 +09:00
Bill Currie
f7bf77c2b7 [qfcc] Support type expressions in dot output
The graph isn't great, but it's better than invalid expression nodes.
2024-04-30 11:16:27 +09:00
Bill Currie
60730a5b12 [qfcc] Prevent dags in type expressions
While they might be ok, I expect them to cause some issues when doing
compile-time evaluations of type expressions, so use of dags seems to be
a premature optimization. However, as the "no dags" flag is propagated
to parent expression nodes, it may prove useful in other contexts.
2024-04-30 10:58:51 +09:00
Bill Currie
966275c294 [qfcc] Remove symbol field from param_t
I have no idea what it was for as nothing actually uses it.
2024-04-26 23:24:32 +09:00
Bill Currie
e8da9924c0 [qfcc] Rework type expression handling
The expression grammar has been tidied up and some basic checks are made
of parameters to the type functions. Also, type parameters are looked up
so parsing now works properly. However, the type parameters are not used
correctly, so function generation doesn't work.
2024-04-26 23:24:32 +09:00
Bill Currie
f7ed55d317 [qfcc] Use type expressions for generic types
This gets type lists and generic name references semi-implemented.
2024-04-26 07:59:21 +09:00
Bill Currie
434bcdebbb [qfcc] Add an expression type for types
No support functions yet, but this should make it easy to manipulate
type "expressions" in the parser for building generic functions.
2024-04-25 23:19:24 +09:00
Bill Currie
45d4b12e7a [qfcc] Implement generic type parsing
The semantics are only partially implemented (generic types not yet
generated), but the generic scope for function declarations seems to be
working as intended in that it gets inserted in the scope chain and
removed at the end of the declaration.
2024-04-25 16:10:43 +09:00
Bill Currie
029b8b1667 [qfcc] Implement vector_type via matrix_type
Since vectors are column matrices, this makes perfect sense and means
matrix_type always gets at least some testing.
2024-04-25 12:41:29 +09:00
Bill Currie
c70da4fc76 [qfcc] Chain matrix and bool types
Ruamoko doesn't really use either yet, but GLSL needs them and this
ironed out a lot of the issues related to adding the types.
2024-04-25 11:22:35 +09:00
Bill Currie
86b3d0db7b [qfcc] Add parameterized types to Ruamoko
The end goal is to allow generic and/or template functions, but this
allows types to be specified parametrically, eg vectors of specific type
and width, with widths of one becoming scalars.

Matrices are currently completely broken as I haven't decided on how to
represent the columns (rows is represented by width (column-major
storage)), and bools are only partially supported (need to sort out
32-bit vs 64-bit bools).
2024-04-24 15:51:29 +09:00
Bill Currie
f3d3bc8ec1 [qfcc] Implement glsl field and swizzle expressions
Ruamoko should have swizzles as well since the same underlying code is
used.
2024-04-22 11:28:13 +09:00
Bill Currie
953be00a72 [qfcc] Use appropriate result type for swizzles
Swizzles now result in a vector of the same width as the swizzle itself
rather than the source vector.
2024-04-22 11:25:10 +09:00
Bill Currie
2bc7cc3d22 [qfcc] Implement a lot of glsl's semantics
Not everything yet, but enough to get type errors in expressions (due to
swizzles not working).
2024-04-22 09:42:02 +09:00
Bill Currie
c62d14da60 [qfcc] Fix some white space 2024-04-22 09:36:47 +09:00
Bill Currie
2dd23f429a [qfcc] Implement glsl syntax
No semantics yet, but qfcc can parse some of QF's shaders. The grammar
mostly follows that in the OpenGL Shading Language, Version 4.60.7 spec,
but with a few less tokens.
2024-04-19 13:54:03 +09:00
Bill Currie
b798cb2603 [qfcc] Move qc/ruamoko keywords into qc-parse.y
This seems to be the right way to go for sharing tokenisation between
Ruamoko and GLSL, while supporting preprocessing for both.
2024-04-19 00:49:47 +09:00
Bill Currie
d1c4c59a57 [qfcc] Handle floats starting with .
eg, .25

My test scene has such a value. Obviously, I'd never compiled it on this
branch since adding PGA physics.
2024-04-18 23:52:52 +09:00
Bill Currie
95a50fefa2 [qfcc] Aggressively cancel terms
While it currently doesn't have any effect on generated code, it proved
to be necessary when experimenting with optimizing the operand of extend
expressions (which proved to produce worse code).

Along with some 0 -> nullptr changes.
2024-04-18 22:46:13 +09:00
Bill Currie
4daa84ed2a [qfcc] Force @return to be live
The change to not split basic blocks on function calls resulted in the
@return def not being live and thus getting dropped when optimizing.
Marking the def as not local forces flow and dags to treat it as global
and thus forced it to be live.
2024-04-18 22:46:13 +09:00
Bill Currie
e7930305cc [qfcc] Add a failing test case for forward return
ProxyView in qwaq's ui stopped working because of the basic block
changes for function calls.
2024-04-18 22:46:13 +09:00
Bill Currie
f390089e66 [qfcc] Treat invalid initialization as just an error
I'm not sure why I treated it as an internal error.
2024-04-18 22:46:13 +09:00
Bill Currie
5191fc26a3 [qfcc] Unify much of the qc and glsl parser interface
This gets the types such that either there is only one definition, or C
sees the same name for what is essentially the same type despite there
being multiple local definitions.
2024-04-18 12:41:24 +09:00
Bill Currie
6266ac4d94 [qfcc] Fix some debian flex incompatibilities
I need upstream flex for its line handling and nicer interface, but
debian's flex is ancient and stuck with the legacy interface, and I want
QF to be buildable on at least sid.
2024-04-18 10:34:18 +09:00
Bill Currie
2f8ffc4862 Merge branch 'master' into wip-ruaspirv 2024-04-18 09:46:10 +09:00
Bill Currie
2d362c445c [qfcc] Move keyword and directive structs
The end goal is to share the tokenisation between the C-like languages,
and maybe even Pascal.
2024-04-17 18:17:08 +09:00
Bill Currie
56f0c3f821 [qfcc] Create a parser file for GLSL
The syntax is not at all correct at this stage (really, just a copy of
Ruamoko), but the keyword table exists (in the wrong place) and the
additional basic types (bool, bvecN and (d)matNxM) have been added.
Boolean base type is currently just int, and matrices have 0 width while
I think about what to use, but finally some progress after several
months' hiatus.
2024-04-17 16:25:43 +09:00
Bill Currie
925dba732c [build] Add an option to enable ubsan
It's disabled by default because it's a runtime thing and I'm not sure I
want to keep it enabled, but it did find some issues (which I've cleaned
up), although it didn't find the problem I was looking for :P
2024-02-21 22:41:08 +09:00
Bill Currie
073c93eebf [qfcc] Don't split basic blocks on function calls
This allows the dags code to optimize the return values, and when I make
the node killing by function calls less aggressive, should make for many
more potential CSE optimizations.
2024-02-21 22:41:08 +09:00
Bill Currie
03b2ee2a25 [qfcc] Delay creation of leaf nodes
Creating leaf nodes early gets in the way of supporting multi-call basic
blocks, and never really seemed right anyway.
2024-02-21 22:41:08 +09:00
Bill Currie
6d6a387a81 [qfcc] Treat move the same as assign in dags
This goes a long way towards allowing basic blocks to include multiple
function calls instead of always ending on a call.
2024-02-21 22:41:08 +09:00
Bill Currie
277feaeb03 [qfcc] Support multivector expression to/from functions
The multivector is first assigned to a temporary then passed or returned
as usual.
2024-02-21 22:41:08 +09:00
Bill Currie
389571cbbe [qfcc] Handle pointer dereferences in offset casts
Offset casts are used heavily in geometric algebra, but doing an offset
cast on a dereferenced pointer (array index) doesn't work well for
assignments. Fixes yet another bug in my test scene :)
2024-02-21 22:41:08 +09:00
Bill Currie
62221b2735 [qfcc] Set the source location for compound init elements
Finally, less bouncing to the compound initializer's closing brace line.
2024-02-21 22:41:08 +09:00
Bill Currie
2be9821a59 [qfcc] Use statement aux def if def not in operands
This takes care of ptrmove instructions, fixing the printf problem in
ptrstructinit. It might even make it possible to not break basic blocks
on function calls, which should make for some more optimization
opportunities, but that can come later.

For this to work properly, it was necessary to avoid converting alias
defs to st_alias nodes.

It also fixes ptrstructinit itself (at first, I hadn't noticed that the
test passed entirely).
2024-02-21 22:41:08 +09:00
Bill Currie
07ef862c52 [qfcc] Put problematic printf into ptrstructinit
It turns out the bug I was chasing in set_poses was not the data getting
lost or incorrect data being read, but the arguments to printf being set
from the parent pose `p` before `p` had actually been set.
2024-02-21 22:41:08 +09:00
Bill Currie
00781160e2 [qfcc] Remove the node from its own edges
This gets rid of the funny little self-loop edge in dags dot files that
has bothered me for a while. It was just because the node to which an
identifier was attached happened to be the parent of the identifier's
leaf node.
2024-02-21 22:41:08 +09:00
Bill Currie
b842913ca9 [qfcc] Remove special treatment for flow/func statements
The fix in bdafdad0d5 for
`while (count--)` never did appeal to me. I think I understood the core
problem at the time, but I hadn't figured out how to use a var's
use/define sets to detect the write-before-read. Using them allows the
special handling for flow control to be removed, making things more
robust. The function call handling has been superfluous since the
Ruamoko instruction set required the auxiliary operands on the call
statements.
2024-02-21 22:41:08 +09:00
Bill Currie
1fb4a556d7 [qfcc] Remove old ptrmove code from dag creation
It doesn't seem to help any, and removing it doesn't break anything
(that is tested), and I'm working on better handling of ptrmove support.
2024-02-21 22:41:08 +09:00
Bill Currie
9a38cdfd78 [qfcc] Clean up dag item counting and init
Not by much really, but using one "aux" count instead of different
types, and using functions to iterate through the statement's aux
operands makes the code easier to read.
2024-02-21 22:41:08 +09:00
Bill Currie
768113d37b [qfcc] Replace expr_file_line with a scoped version
Two birds with one stone: eliminates most of the problems with going
const-correct with expr_t, and it make dealing with internally generated
expressions at random locations much easier as the set source location
affects all new expressions created within that scope, to any depth.
Debug output is much easier to read now.
2024-02-21 22:41:08 +09:00
Bill Currie
6e8c98433a [qfcc] Fix a missed const type_t 2024-02-21 22:41:08 +09:00
Bill Currie
b9aad99381 [qfcc] Clean up warning and notice return types
They never needed to return expr.
2024-02-21 22:41:08 +09:00
Bill Currie
131ad9013c [qfcc] Tidy up expr_assign_copy a little
It proved that expr_assign_copy wasn't the cause of any problems, but I
cleaned up some of its types a little.
2024-02-21 22:41:08 +09:00
Bill Currie
35e2044bfa [qfcc] Fix nested compound initializers
Add static array indexing. This gets the compoundinit test working.
2024-02-21 22:41:08 +09:00
Bill Currie
19965e7cbd [qfcc] Add a failing test case for compound initializers
It turns out that compound intializers break on when the nesting level
increases (eg, initializing vectors in structs in arrays).
2024-02-21 22:41:08 +09:00
Bill Currie
1b4db639af [qfcc] Handle quaternion (in)equality tests
I'm surprised this didn't come up earlier.
2024-02-21 22:41:08 +09:00
Bill Currie
0e7c80e9fe [qfcc] Use ud-chains for ptrmove pointer analysis
ptrmove was not treating its indirect source operand as used because the
pointer wasn't checked for its source.

This fixes part of ptrstructinit, but there's a lot more breakage in
that test: it looks like all sorts of fun with arrays.
2024-02-21 22:41:08 +09:00
Bill Currie
ab69e65e02 [qfcc] Add failing test case for pointer struct initialization 2024-02-21 22:41:08 +09:00
Bill Currie
3a22091455 [qfcc] add pointer validity check function to test-harness 2024-02-21 22:41:08 +09:00
Bill Currie
192eb11235 [qfcc] Dereference most array types properly
This should prevent some fun bugs in the future (it's what necessitated
the const-correct patch).
2024-02-20 16:47:06 +09:00
Bill Currie
f0dfe47a32 [qfcc] Make type_t mostly const-correct
There were a few places where some const-casts were needed, but they're
localized to code that's supposed to manipulate types (but I do want to
come up with something to clean that up).
2024-02-20 16:47:04 +09:00
Bill Currie
6e71aadfa4 [qfcc] Use dereference_type for initialized arrays
In particular, when the size is inferred from the compound initializer.
Fixes a fun "incomplete type" error.
2024-02-20 16:46:02 +09:00
Bill Currie
fa5c3ab12e [qfcc] Support arrays in dereference_type
This is needed to get the correct type from arrays of typedefed
elements.
2024-02-20 16:46:02 +09:00
Bill Currie
827ca3cf27 [qfcc] Make array_type const-correct
As such... need to cast away the const when calling append_type (which
should probably become internal).
2024-02-20 16:46:02 +09:00
Bill Currie
3d26aafbea [qfcc] Unalias def ops too
When I implemented the st_alias handing (d8a78fc849) I was
unsure if I needed to unalias aliased defs too, but it turns out to have
been necessary: this is what caused my 2d PGA dynamics test to blow up
strangely due to the GA vector loaded from an array into a local
variable getting the local var replaced by a temp but the var itself
being read later in the code (uninitialized variable due to incorrect
optimization... oops).
2024-02-20 16:46:02 +09:00
Bill Currie
37fe0bc4ed [qfcc] Fix some 2d PGA bugs
When sum_expr gets a null expression as one of its args, it simply
returns the other arg, but that arg needs to have the correct type
applied.

Handle zero (null result) cross product in bivector geometric product.

Clean up types in bivector * vector geometric product.
2024-02-20 16:46:02 +09:00
Bill Currie
946211bca2 [qfcc] Use a better name for a var
Looking at the code afterwards, I realized flips would be a far better
name than mask. Not sure what I was thinking when I wrote the code.
2024-02-20 16:46:02 +09:00
Bill Currie
40f04ff33e [qfcc] Implement undual and use for regressive product
I'm not sure the regressive product is right (overall sign), but that's
actually partly a problem in the math itself (duals and the regressive
product still get poked at, so it may be just a matter of
interpretation).
2024-02-20 16:46:02 +09:00
Bill Currie
10861986fb [qfcc] Add tests for the hodge dual 2024-02-20 16:46:02 +09:00
Bill Currie
d319ce6106 [qfcc] Support multi-vector comparison
I'm not sure anything other than == or != has much meaning on anything
but scalars and pseudo scalars, but all comparisons are supported as a
simple boolean test. Any missing components are assumed to be 0. If
nothing else, it makes unit tests easier to write.
2024-02-20 16:46:02 +09:00
Bill Currie
11b247918e [qfcc] Find current algebra context for duals
Now finding the dual of a scalar works, too (need a context as otherwise
the pseudo-scalar is unknown).
2024-02-20 16:45:55 +09:00
Bill Currie
e56b4a3a6d [qfcc] Strip off algebra types internally
It turns out the algebra types make expression dag creation much more
difficult resulting in missed optimizations (eg, recognizing `a × a`).
This fixes the dead cross products in `⋆(s.B × ⋆s.B)`
2024-02-08 13:56:59 +09:00
Bill Currie
cf756eb1a0 [qfcc] Emit statements for expressions only once
The switch to using expression dags instead of trees meant that the
statement generator could traverse sub-expressions multiple times. This
is inefficient but usually ok if there are no side effects. However,
side effects and branches (usually from ?:, due to labels) break: side
effects happen more than once, and labels get emitted multiple times
resulting in orphaned statement blocks (and, in the end, uninitialized
temporaries).
2024-02-08 13:56:59 +09:00
Bill Currie
701b0f3992 [qfcc] Print block label expressions first
Just running through the list of expressions in a block expression
results in label expressions within the block getting printed by
expressions that reference them and thus don't receive the correct next
pointer and wind up pointing to themselves. Printing the labels first
ensures they have the correct next pointer. However, I suspect there are
other ways things will get tangled.
2024-02-08 13:56:59 +09:00
Bill Currie
5fef8e6edb [qfcc] Implement quaternion multiplication
I'm surprised it took almost two years to discover that I had no
quaternion multiplications in any test code, but getting an ICE for a
quaternion-vector product, and the Hadamard product for
quaternion-quaternion was a bit of a nasty surprise.
2024-02-01 11:00:27 +09:00
Bill Currie
8994042a47 [qfcc] Support expanding constant expressions
This makes a slight improvement to the commutator product in that it
removes the expand statement, but there's still the problem of (a+a)/2.
However, at least now the product is correct and slightly less abysmal.
2024-01-19 15:36:47 +09:00
Bill Currie
aa9be53b6e [qfcc] Include divide by two in commutator product
I had forgotten this, and of course didn't have any tests.
2024-01-19 15:26:18 +09:00
Bill Currie
5c22ba118c [qfcc] Support aliasing constants
This takes advantage of evaluate_constexpr to do all the work. Necessary
for use of basis blade constants in algebra contexts (avoids an internal
error).
2024-01-18 13:08:21 +09:00
Bill Currie
bfa7c1722a [build] Implement tracy memory zones
This proved to be quite the challenge, and is probably rather fragile,
but it does seem to work, and might help with tracking down memory
leaks.
2024-01-05 11:50:48 +09:00
Bill Currie
18c0ea3657 [qfcc] Ensure one is the correct type for incop expressions
This makes for nicer generated code when the incop (especially post) is
used in a condition.
2023-12-20 23:10:17 +09:00
Bill Currie
dd183d3ba6 [qfcc] Handle signed-unsigned int comparison better
This fixes the upostop-- test by auto-casting implicit constants to
unsigned (and it gives a warning for signed-unsigned comparisons
otherwise). The generated code isn't quite the best, but the fix for
that is next.

Also clean up the resulting mess, though not properly. There are a few
bogus warnings, and the legit ones could do with a review.
2023-12-20 23:09:06 +09:00
Bill Currie
54839db826 [qfcc] Add some tests for postop--
Interestingly, intval-- works properly, but unsignedval-- does not (test
case fails). This is why menus blow up when opening the load/save
sub-menu.
2023-12-20 21:50:51 +09:00
Bill Currie
65bf3ab459 [qfcc] Switch to using the internal preprocessor
It's actually good enough for building qfcc: it fails a couple of
obscure c23 preprocessor examples that can be ignored for now (the rest
of QF builds without any apparent issue).
2023-11-23 13:33:44 +09:00
Bill Currie
2fd2a7086f [qfcc] Save disabled preproc expression debug 2023-11-23 13:33:12 +09:00
Bill Currie
5e02716a01 [qfcc] Save force-expanding function-type arg macros
The expansion is necessary for the final test in preproc-2.r, but breaks
preproc-1.r because the closing ')' is *not* visible to collect_args
(its assumption is incorrect). This needs reworking (and probably
rethinking) of the entire macro argument collection, but I need a little
break from the preprocessor (and it's good enough for *most* uses), so
I'm adding the code (disabled) in order to avoid losing it and my notes
about the problem.
2023-11-23 13:28:48 +09:00
Bill Currie
4480453861 [qfcc] Fail function-type macro expansion early
That is, if anything other than '(' (even a macro/argument that expands
to '(')is seen while checking for a function-type macro, the expansion
fails. This gets preproc-1.r working properly.
2023-11-23 13:26:02 +09:00
Bill Currie
4ed25ed616 [qfcc] Correct some misuses of scanner
It really affected only collect_args, but in fixing that I noticed I was
very inconsistent with scanner's type (should be yyscan_t or void *, but
not yyscan_t *).
2023-11-22 11:54:14 +09:00
Bill Currie
8c0b30e4e6 [qfcc] Force object-type macro expansion in args
This seems to get __FILE__ and __LINE__ working as expected, at least
when not used in __VA_OPT__.
2023-11-22 00:31:54 +09:00
Bill Currie
bf44ff3194 [qfcc] Base default object file name on output file
There's no guarantee the source file is in a writable directory (in
fact, it is very definitely in a read-only directory when running
`make distcheck`). However, it is reasonable to assume the output file
is being written to a writable directory thus default the object file
directory to that of the output file, but still use the source file's
name for the object file name.

Fixes #51
2023-11-20 14:19:01 +09:00
Bill Currie
ce6e7a32fd [qfcc] Use place-markers for some empty expansions
This gets most of the second preprocessor test working, apparently just
some problems with macro arguments not getting expanded for ## (unless
there's more lurking, of course, which I know there is for __LINE__).
2023-11-19 14:08:09 +09:00
Bill Currie
eb6a5b771a [qfcc] Get __VA_OPT__ non-expansion working
For most cases: function-type macros aren't properly checked for being
empty, but this gets a lot of the next preprocessor test working.
2023-11-19 02:10:44 +09:00
Bill Currie
5c0b2aa891 [qfcc] Clean up some cruft from recent __VA_OPT__ changes
Using a special token id for __VA_OPT__ means the macro is no longer
needed (need to improve checks for misuse of __VA_OPT__, though).
2023-11-18 19:56:19 +09:00
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