Commit graph

4909 commits

Author SHA1 Message Date
Bill Currie
cfc5e0b5fd [qfcc] Handle convert symbols when processing
I'm pretty sure I don't want this getting into the target code, so
handling it early (like in the immediate processing path) seems good.
2024-11-21 19:18:13 +09:00
Bill Currie
c3bbd85857 [qfcc] Skip void param for spir-v
Functions declared with `(void)` don't actually have any parameters, so
ensure no parameters are emitted for such functions.
2024-11-21 19:18:13 +09:00
Bill Currie
744c3ff1d6 [qfcc] Handle reference params/args for overloaded functions
Reference vs value parameters do not resolve overloaded functions.
2024-11-21 19:18:13 +09:00
Bill Currie
663aeb16fd [qfcc] Enable color/texture vector component names
Yet another step closer to glsl support.

While it wasn't correct anyway (neither vectors nor quaternions are
colors), the additional names broke q.s in the quaternion.r test, likely
due to there being two s fields.
2024-11-21 19:18:13 +09:00
Bill Currie
08bdc0794a [qfcc] Improve expression line tracking
Missed a few places in expr_process.
2024-11-21 19:18:13 +09:00
Bill Currie
fb497cd0b2 [qfcc] Clean up the errors for some layout qualifiers
Nothing's actually implemented, but once I fix a couple more errors I'll
be able to work on that.
2024-11-21 19:18:13 +09:00
Bill Currie
c16a8411bc [qfcc] Extend handles to support image types
Images are just handles, but they have additional type data like
dimensionality, array, etc that distinguishes between the image types.
2024-11-21 19:18:13 +09:00
Bill Currie
03c47f8af7 [qfcc] Support (partially) attributes on types
For now, it's just recording that type type has attributes (encoding
begins with %) and resurrecting types_same which is used only when
matching with types with attributes, so there's still a fair bit of work
to do.
2024-11-21 08:30:47 +09:00
Bill Currie
5e605aee36 [qfcc] Clean up some overzealous const correctness
I'm not sure why I made those functions take const type_t *, but they
didn't need it. There's still a relevant fixime in find_handle, but I
haven't decided how to fix that one just yet.
2024-11-21 08:30:47 +09:00
Bill Currie
28a85b1624 [qfcc] Fix some crashes when parsing bsp_turb.frag
Incorrect type accesses and unnamed interfaces.
2024-11-19 17:15:42 +09:00
Bill Currie
34b94aec96 [qfcc] Set binding and set attributes
Not sure if this will work, but it does fix some error and notice noise.
2024-11-19 17:13:38 +09:00
Bill Currie
59c8e8e907 [qfcc] Don't check for redefs of unnamed symbols
Both glsl and spir-v need this for non-instanced interface blocks.
2024-11-19 17:12:03 +09:00
Bill Currie
128682624e [qfcc] Process array sub-expressions 2024-11-19 17:10:37 +09:00
Bill Currie
943cf9659b [qfcc] Make requiring @overload a language feature
Ruamoko still needs @overload (for now), but now glsl does not.
2024-11-18 22:46:14 +09:00
Bill Currie
393f669864 [qfcc] Ensure function types are complete
All types need non-zero alignment, width and columns.
2024-11-18 22:45:08 +09:00
Bill Currie
9add468462 [qfcc] Improve interface block declaration
I'm sure there are still some warts, but interface declaration now works
for spir-v and a real vertex shader can be compiled (albeit one that
doesn't use matrices).
2024-11-18 21:41:33 +09:00
Bill Currie
3dc22d8cd6 [qfcc] Support spir-v vector expressions even better
It turned out I did need direct vector expression support: the previous
commit handled only assignments, not sub-expressions.
2024-11-18 15:47:22 +09:00
Bill Currie
0771d6b3be [qfcc] Support assignment to field members in spirv
And now something like `a.y += 1` works.
2024-11-18 15:29:58 +09:00
Bill Currie
9b2240aaf8 [qfcc] Implement vector expressions for spir-v
And fix a little goof with VectorShuffle (c&p coding bites again).
2024-11-18 14:55:32 +09:00
Bill Currie
39514e4f3d [qfcc] Implement swizzle and arrays for spir-v
Arrays done via the field code since they use the same opcode and logic.
For now swizzles are just swizzles and don't support zeroing or negating
(but doing one or the other (not both) should be easy).
2024-11-18 12:29:25 +09:00
Bill Currie
73a8404684 [qfcc] Alias pointer etc to int for !
This fixes the calluse2 test.
2024-11-18 11:51:10 +09:00
Bill Currie
13f25af881 [qfcc] Use the called function for ruamoko progs
This was a nasty one to find as it was the unary expression rewrite that
triggered the bug (by causing tighter if tests to be generated, which
surprised me: I wasn't even trying for better if tests). I'm actually
not sure why things worked at all, but this fixes the crashes and other
weird behavior resulting from calls to performSelector.

Two tests are included because I found a separate bug (not fixed yet,
tested by calluse2) while getting the calluse test working.
2024-11-18 03:42:26 +09:00
Bill Currie
28e8697ae6 [qfcc] Implement compound initializers for spir-v
They don't yet support designators, and vectors aren't treated the way I
want, but my little test works correctly.
2024-11-17 22:45:18 +09:00
Bill Currie
91a93d7803 [qfcc] Add type_count() function
It returns the number of elements in a type (so something like `countof`
(hopefully that's what the up-coming C feature will be called) can be
implemented), but it applies to structs, vectors, etc (eg, 9 for mat3).
2024-11-17 16:12:27 +09:00
Bill Currie
d082d39e17 [qfcc] Use count for number of array elements
I got tired of seeing size when count is the real intent (mistake copied
from C++).
2024-11-17 15:27:10 +09:00
Bill Currie
2115e962fe [qfcc] Support typed compound initializers
Explicitly typed compound initializers are what C uses and allows them
to initialized `auto` vars and even pass through `...`. Not tested yet
other than ensuring existing tests didn't break.
2024-11-17 13:17:36 +09:00
Bill Currie
a7f9d96a02 [qfcc] Implement select (if) constructs
And break/continue too, since my test for them required select.
2024-11-16 23:13:46 +09:00
Bill Currie
93d116cf16 [qfcc] Rename body_first to do_while
Since loops are always while or do-while (for loops are just while loops
with an init block), it made more sense in the end.
2024-11-16 23:10:54 +09:00
Bill Currie
2ef72f745d [qfcc] Improve glsl scope handling
While storing the scope in a block was the right idea, the
implementation wasn't quite right as it resulted in the current scope
getting set at the wrong times resulting in incorrect symbol access when
shadowing was in effect.
2024-11-16 20:14:51 +09:00
Bill Currie
3797b1083b [qfcc] Implement loops for spir-v
Mostly, at least: continue and break aren't functional yet, and for
loops haven't been tested, but while and do-while seem to work.
2024-11-16 19:29:41 +09:00
Bill Currie
89dac4a1b8 [qfcc] Create scopes for while and for loops in glsl
I guess that was a mental FIXME for later, but later came sooner. The
declaration doesn't seem to be working properly, but I'll worry about
that later when I can get some automated tests going.
2024-11-16 19:29:41 +09:00
Bill Currie
01d3c60fe0 [qfcc] Encode param qualifiers in function type
I'm not sure whether I'll stick with the current encoding, but it turns
out it's necessary to separate function types on parameter qualifiers (I
was half expecting that, though).
2024-11-16 19:29:41 +09:00
Bill Currie
22794caf11 [qfcc] Support vector value expressions for spir-v
It seems constant initializers are different from constants in an
expression.
2024-11-16 18:54:31 +09:00
Bill Currie
27afd608f1 [qfcc] Emit bool types
Because spir-v's bools are abstract and have no specific size, bool and
lbool need to both have the same spir-v id, as do the vector
counterparts, so when one gets emitted, the id is copied to the mirror
type.
2024-11-15 13:09:26 +09:00
Bill Currie
ebb3ec592a [qfcc] Implement bool and lbool types
Since spir-v needs actual bools for its conditional instructions, the
time to do bool properly finally came. As expected, the changes caused
quite a mess, but Ruamoko now does bool/true/false.
2024-11-15 12:36:08 +09:00
Bill Currie
ad36d2cd50 [qfcc] Clean up some 0/nullptr in type.c 2024-11-15 10:50:03 +09:00
Bill Currie
7ad6aff928 [qfcc] Implement cast and ?: expressions for spir-v
Unfortunately, there are some bugs with the result of comparisons
(getting int instead of bool in the spir-v).
2024-11-15 10:49:07 +09:00
Bill Currie
81cc736683 [qfcc] Add is_signed and is_unsigned type check functions
Makes implementing casts for spir-v much easier.
2024-11-15 10:49:07 +09:00
Bill Currie
d6d44b62d9 [qfcc] Use bool for type is_* functions
More clarity often helps.
2024-11-15 10:49:07 +09:00
Bill Currie
dc85a4df4b [qfcc] Specify entry-point interface variables
Yet another step closer to having a working glsl compiler.
2024-11-14 20:58:21 +09:00
Bill Currie
c5613ba1e4 [qfcc] Use tagged references for spir-v storage
The tags might have other uses thus the generic term, but this allows
spir-v variables to be declared with the correct pointer storage
classes. Now spirv-val is complaining about my interfaces, so progress :)
2024-11-14 18:37:38 +09:00
Bill Currie
fa42478af6 [qfcc] Ensure final block is terminated
Which for a function means return or unreachable (though I suppose
branches might work).
2024-11-14 09:04:48 +09:00
Bill Currie
5033dc9a12 [qfcc] Use staging vars for non-const params
Non-const params use references and so need somewhere to store the
arguments. Local variables could be passed directly (parameters normally
require function storage class, which local variables are), but that's a
potential optimization for later.
2024-11-14 09:04:48 +09:00
Bill Currie
698430630d [qfcc] Emit spir-v global variables
This required adding a `var` symbol type. For now, it holds just the
storage class, but it might be good for the initializer, too.

Also, clean up some pointer/reference inconsistencies.
2024-11-14 09:04:48 +09:00
Bill Currie
ae4d87b969 [qfcc] Dereference cast expression 2024-11-13 19:03:20 +09:00
Bill Currie
52d38ce8a6 [qfcc] Set struct member ids
The id matches the member's order in the struct's fields (needed for
spir-v access chains).
2024-11-13 14:25:25 +09:00
Bill Currie
14f428a81d [qfcc] Don't dereference pointer for field access
As I suspected, the dereference was premature, although it can act as an
optimization when multiple fields of an object are accessed, though it
won't work for writes to lvalues (which means my fixes for spir-v
code-gen currently don't work for lvalue writes). Still, progress. Need
to get field member ids set.
2024-11-13 14:05:46 +09:00
Bill Currie
fb58c1aace [qfcc] Implement field access for spir-v
It turns out my field access processing works well enough for vectors.
I'm not sure about pointer access as I may have done the pointer
dereference prematurely.
2024-11-13 13:36:57 +09:00
Bill Currie
549203d05d [qfcc] Correct a comment 2024-11-13 13:36:46 +09:00
Bill Currie
410317fdf7 [qfcc] Mark spirv variable declarations as lvalues
I'm not sure this is always correct, but it does respect `const`.
2024-11-13 12:16:43 +09:00
Bill Currie
b43fe9ddc5 [qfcc] Process field expressions
I'm not sure they work yet as the spirv output doesn't support them yet,
but after an lvalue fix (next), this gets field accesses to code-gen.
2024-11-13 12:15:24 +09:00
Bill Currie
7b80ec2a14 [qfcc] Support field expressions in dot output
Some other types were added with null functions to make things a little
easier when I get around to implementing them.
2024-11-13 12:14:05 +09:00
Bill Currie
e4e04b183a [qfcc] Switch is_lvalue to use bool instead of int
Self documentation for the win.
2024-11-13 12:13:05 +09:00
Bill Currie
6649d8238e [qfcc] Process glsl parameters correctly
This gets parameter qualifiers to the parameters and thus `const`
parameters work as expected (even passes validation). However,
in/out/inout do not work yet due to incorrect setup of the function
call: spir-v wants them to be passed by reference (pointers) rather than
by value.
2024-11-12 23:49:47 +09:00
Bill Currie
32bf9e9435 [qfcc] Generate spir-v function calls
The function parameter and argument types are a mess with respect to
references (and thus calls don't pass validation) but the generated code
seems to be otherwise correct.
2024-11-12 18:24:40 +09:00
Bill Currie
f9345caeb3 [qfcc] Clean up spirv instruction generation
This should make it easier to declare local variables on the fly (needed
for function arguments).
2024-11-12 01:26:20 +09:00
Bill Currie
8a3a39be68 [qfcc] Fix a missed local var use
I'd intended on using arg_space instead of current_func->arguments, but
missed one spot.
2024-11-12 01:24:31 +09:00
Bill Currie
a68aaad307 [qfcc] Clean up some arg/param confusion
Only in expr_call.c, but that's probably where it's at its worst.
2024-11-10 23:34:07 +09:00
Bill Currie
819d0fc3be [qfcc] Split up build_function_call
It wasn't quite as nasty as I thought, but splitting it up helps add a
little self documentation and, possibly more importantly, simplify
navigation through the type checks and argument setup.
2024-11-10 16:48:01 +09:00
Bill Currie
bcfd1b7660 [qfcc] Split out the call related expression code
I've long felt build_function_call was getting a bit big, and expr.c
especially so. This should make it easier to rewrite build_function_call
for dealing with target-specific code. As a bonus, the int through ...
warning is already cleaned up.
2024-11-04 20:28:30 +09:00
Bill Currie
a0845e65fb [qfcc] Use temp expressions as spir-v id carriers
spir-v uses SSA, so temps cannot be assigned to directly, so instead use
the temp expression as a reference for the result id of the rhs of the
assignment. This would get function calls working if the they actually
emitted any code (right now, just a place-holder id so spirv-dis doesn't
fall over).
2024-11-04 16:42:44 +09:00
Bill Currie
95cedd879a [qfcc] Remove a stale macro
I don't remember what it was for even, other than somehow dereferencing
a pointer def.
2024-11-04 16:38:51 +09:00
Bill Currie
6358143795 [qfcc] Clean up the local_expr global
I never liked it and it made things a little difficult for spir-v.
2024-11-04 16:38:07 +09:00
Bill Currie
5bf9d3c57c [qfcc] Implement spir-v assignment to vars
Since actual variables are always references, they need to written via
OpStore.
2024-11-04 12:53:21 +09:00
Bill Currie
6e5b13c6f6 [qfcc] Clean up spir-v function generation
Now spirv-val has something to chew on.
2024-11-04 12:33:59 +09:00
Bill Currie
9da3ac3fdc [qfcc] Clean up some int/bool/nullptr cruft 2024-11-04 12:29:30 +09:00
Bill Currie
9d7680228c [qfcc] Attempt to generate code for functions
It sorta kinda works, but there's a lot of bugs, and a lot more stuff to
implement, and the result doesn't pass validation yet.
2024-11-03 23:45:54 +09:00
Bill Currie
04560f61a6 [qfcc] Separate out backend variable declaration
This time, rua/v6p/v6 share the code properly, since it's only spirv
that needs something specific at this stage.
2024-11-03 18:06:25 +09:00
Bill Currie
88c67b3575 [doc] Update doxygen config via doxygen
Also, fix some missing docs. Unfortunately, there are still some
problems (incorrect resolution for multiple files/functions with the
same name, and a bug with doxygen's verbatim/code blocks).
2024-11-03 16:58:10 +09:00
Bill Currie
c8fbb93424 [qfcc] Separate out backend function building
It turns out that emit_function should have been (something like)
build_code and had the stack/locals related code taken along with it.
2024-11-03 16:58:10 +09:00
Bill Currie
a22d2a9526 [qfcc] Move guts of emit_function to per-target code
Ruamoko and v6(p) have their own copies despite being (currently) the
same, and spir-v's is currently empty, but now targeting spir-v doesn't
try to emit ruamoko code.
2024-10-26 23:30:37 +09:00
Bill Currie
95eaa4c65e [qfcc] Implement 64-bit bool
I'm not certain it's what I want, but this fixes the double.r test which
began failing with the non-scalar unary ops change.
2024-10-26 22:16:02 +09:00
Bill Currie
8ac4716f67 [qfcc] Return correct type for non-scalar unary ops
At least, for non-constants. Constants are still broken (badly,
actually).
2024-10-26 13:44:49 +09:00
Bill Currie
295cc0e0b2 [qfcc] Make non-const params references for spir-v
The whole reason for the reference work.
2024-10-26 13:33:39 +09:00
Bill Currie
af42b8e221 [qfcc] Support references in expressions
While a reference var can't be initialized yet, using them seems to work
in that they get dereferenced when the value needs to be read or written
(though I haven't seen any generated code for them yet).
2024-10-26 13:33:18 +09:00
Bill Currie
6db1942bb1 [qfcc] Make max_params a code option
Rather than relying on progsversion (I think this also fixes a bug with
max params when targeting ruamoko).
2024-10-11 10:01:45 +09:00
Bill Currie
f3181d64d0 [qfcc] Remove strange split in function arg handling
I don't know why the last argument was handled differently. I suspect it
was a hold-over from before I added list handling. Removing it seems to
have made no significant difference (all tests pass still).
2024-10-10 19:17:24 +09:00
Bill Currie
d72113f5cb [qfcc] Select correct generic function for references
When the parameter is a reference, implicit casting is not allowed, but
when the parameter is by value and the argument is a reference, the
argument is dereferenced and promotion is allowed.

However, this covers only the selection of generic functions. It doesn't
deal with otherwise overloaded functions, nor does it do the actual
dereferencing or address taking.
2024-10-09 13:33:38 +09:00
Bill Currie
421796b9ea [qfcc] Unalias type before checking for pointer/reference
Before adding references, looking at just type.type was ok whether or
not the type was an alias, but checking the deref flag needs the type to
be basic and not alias.
2024-10-09 11:31:00 +09:00
Bill Currie
a61e20ab20 [qfcc] Add parameters to spir-v function scopes
Simple functions now get to the code-gen phase (where they fail since
it's the wrong for other reasons). Parameter types aren't right for
spir-v yet as non-const params need to be references.
2024-10-08 11:58:24 +09:00
Bill Currie
9cd480640d [qfcc] Defer return expressions 2024-10-08 11:46:52 +09:00
Bill Currie
c3f11ba754 [qfcc] Add basic (type) support for references
I realized that spir-v pointers are essentially references (the way
they're used) since OpVariable requires a pointer type rather than the
base type. Thus, under the hood, references are just pointers with
automatic dereferencing. However, nothing uses references yet, and I
expect to run into issues with is_pointer vs is_reference vs is_ptr
(high-level pointer, reference, low-level pointer, respectively).
2024-10-08 11:45:51 +09:00
Bill Currie
dc878dbb4c [qfcc] Skip scope creation for function bodies
Since begin_function creates the parameter and locals scopes, there's no
need for another scope for the function body. This fixes the detection
of shadowed parameters for Ruamoko and QuakeC.
2024-10-07 09:55:21 +09:00
Bill Currie
3c7bd275f3 [qfcc] Detect shadowed parameters in glsl
Unfortunately, it breaks detection in Ruamoko and QuakeC.
2024-10-07 09:51:03 +09:00
Bill Currie
dae442c91e [qfcc] Move function scope building into target
This nicely separates Ruamoko and V6p function scope creation, and then
makes spir-v's a no-op (for now).
2024-10-07 09:48:44 +09:00
Bill Currie
0bc58246b1 [qfcc] Add a passing test for shadowed params
It's currently passing for Ruamoko, but glsl doesn't catch shadowed
params. There's a bit of a conflict and I want to make sure I don't lose
the checking.
2024-10-07 09:29:14 +09:00
Bill Currie
0cf8e7cb41 [qfcc] Start properly separating target code
I'd gotten tired of all the convoluted progs version checks, and with
the addition of spirv, they're not even always relevant, and adding C
(when I get to it) will make things even worse. However, for now the
first victim is just the parameter/return value size check.
2024-10-06 14:29:02 +09:00
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