Commit graph

105 commits

Author SHA1 Message Date
Bill Currie
ed88152f85 [qfcc] Get type attribute working for computed types
It even handles errors :)
2025-01-14 01:02:31 +09:00
Bill Currie
625c53180f [qfcc] Propagate rua_ctx_t to more functions
It will eventually get to most places, but this set is necessary for
using expr_process in expr_type.c
2024-12-23 22:03:47 +09:00
Bill Currie
626680f22f [qfcc] Handle return in inline functions
The code is pretty lousy in that it assumes there's only one `return`
and it's the end of the function, and the generated code is even worse
(too many load/store ops in the spir-v), but it looks like it at least
works. It does pass validation.
2024-12-12 11:08:30 +09:00
Bill Currie
6840a208b9 [qfcc] Implement inline function calls
They're buggy in that the defspaces for parameters and locals are
incorrect (they need to point to the calling scope's space). Also,
parameters are not yet hooked up correctly. However, errors (because I
need to allow casts from scalars to vectors) do get handled.
2024-12-11 03:16:15 +09:00
Bill Currie
36cf1f948e [qfcc] Add bypass scopes
Because the symbol tables for generic functions are ephemeral (as such),
they need to be easily removed from the scope chain, it's easiest if
definitions are never added to them (instead, they get added to the
parent symbol table). This keeps handling of function declarations or
definitions and their parameter scopes simple as the function gets put
in the global scope still, and the parameter scope simply gets
reconnected to the global scope (really, the generic scope's parent)
when the parameter scope is popped within a generic scope.
2024-12-11 03:06:50 +09:00
Bill Currie
306306fc5a [qfcc] Move semantic processing into build_code_function
In the end, I could have done the null context check for pascal, but I
think the xvalue support will come in handy for implementing inline
functions.
2024-12-08 20:01:06 +09:00
Bill Currie
4a8c53aa6f [qfcc] Add partial support for renamed builtins
The back-end support for renamed builtins (fte's = #0:name) was needed
for pascal functions because the internal name is now prefixed with an @
to allow the lvalue/rvalue selection of behavior for function symbols
2024-12-08 19:13:49 +09:00
Bill Currie
ec436ee65e [qfcc] Put definition for generic functions in genfunc
It is then copied to the metafunc when finding the actual function to
call. Fixes the last definition defining all instances.
2024-12-05 16:08:39 +09:00
Bill Currie
1e5d500f8b [qfcc] Implement intrinsic functions in spir-v
Only a tiny handful of glsl functions are implemented (dot, cross,
sqrt), but the system works, both via generics and regular overloads.
2024-11-25 01:39:27 +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
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
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
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
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
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
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
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
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
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
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
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
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
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
2f8ffc4862 Merge branch 'master' into wip-ruaspirv 2024-04-18 09:46:10 +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
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
210a925be4 [qfcc] Make expressions const-correct
Or at least mostly so (there are a few casts). This doesn't fix the
motor bug, but I've wanted to do this for over twenty years and at least
I know what's not causing the bug. However, disabling fold_constants in
expr_algebra.c does "fix" things, so it's still a good place to look.
2023-09-29 10:15:59 +09:00
Bill Currie
e2c1da9b6a [qfcc] Create du-chains from ud-chains
I'm not certain this is correct, but it seems to me that du-chains are
the same information as ud-chains, but from the defining statement's
point of view instead of that of the using statement.
2023-06-04 11:24:52 +09:00
Bill Currie
9247ab91fd [qfcc] Calculate ud-chains
The first use will be pointer analysis for function arguments where the
argument points to an array to mark the array as live, but I'm sure
there'll be plenty of other uses.
2023-05-20 08:49:51 +09:00
Bill Currie
0c116b8ff0 [qfcc] Record actual statements in a function
This is to help with building ud-chains as I suspect I won't be all that
interested in defs from the dummy blocks.
2023-05-19 17:50:19 +09:00
Bill Currie
7b7b2ef000 [qfcc] Add flow defs for parameters
Needed for proper analysis (ud-chains etc). Of course, it was then
necessary to remove the parameter defs from the uninitialized defs.

Also, plug a couple of memory leaks (forgot to free some temporary
sets).
2023-05-14 13:40:40 +09:00
Bill Currie
cee00c8243 [qfcc] Fix declarators for pointers/functions/arrays
I had messed up the handling of declarators for combinations of pointer,
function, and array: the pointer would get lost (and presumably arrays
of functions etc). I think I had gotten confused and thought things were
a tree rather than a simple list, but Holub set me straight once again
(I've never regretted getting that book). Once I understood that, it was
just a matter of finding all the places that needed to be fixed. Nicely,
most of the duplicated code has been refactored and should be easier to
debug in the future.
2023-03-09 02:22:23 +09:00
Bill Currie
3a297c70b3 [qfcc] Handle qc function field parameters
The type system rewrite had lost some of the checks for function fields.
This puts the actual code in the one place and covers parameters as well
as globals.
2023-02-14 12:45:04 +09:00
Bill Currie
fc7c96d208 [qfcc] Support C's full type system
Along with QuakeC's, of course. This fixes type typeredef2 test (a lot
of work for one little syntax error). Unfortunately, it came at the cost
of requiring `>>` in front of state expressions on C-style functions
(QuakeC-style functions are unaffected). Also, there are now two
shift/reduce conflicts with structs and unions (but these same conflicts
are in gcc 3.4).

This has highlighted the need for having the equivalent of the
expression tree for the declaration system as there are now several
hacks to deal with the separation of types and declarators. But that's a
job for another week.

The grammar constructs for declarations come from gcc 3.4's parser (I
think it's the last version of gcc that used bison. Also, 3.4 is still
GPL 2, so no chance of an issue there).
2023-02-14 12:45:04 +09:00
Bill Currie
2c8bec27c7 Fix a pile of warnings for gcc 12
Most were pretty easy and fairly logical, but gib's regex was a bit of a
pain until I figured out the real problem was the conditional
assignments.

However, libs/gamecode/test/test-conv4 fails when optimizing due to gcc
using vcvttps2dq (which is nice, actually) for vector forms, but not the
single equivalent other times. I haven't decided what to do with the
test (I might abandon it as it does seem to be UD).
2022-07-31 17:13:26 +09:00
Bill Currie
70af362562 [qfcc] Unify temp def, return value and parameter sizes
In working with vectors and matrices while testing the scene wrappers, I
found that there was a fair bit of confusion about how large something
could be. Return values can be up to 32 words (but qfcc wasn't aware of
that), parameters were limited to 4 words still (and possibly should be
for varargs), and temp defs were limited to 8 words (1 lvec4). Temps are
used for handling return values (at least when not optimizing) and thus
must be capable of holding a return value, and passing large arguments
through *formal* parameters should be allowed. It seems reasonable to
limit parameter sizes to return value sizes.

A temp and a move are still used for large return values (4x4 matrix),
but that's an optimization issue: the code itself is at least correct.
2022-02-15 08:39:20 +09:00
Bill Currie
52a399daeb [qfcc] Update sizes and alignments for dvec4 and friends
dvec4, lvec4 and ulvec4 need to be aligned to 8 words (32 bytes) in
order to avoid hardware exceptions. Rather than dealing with possibly
mixed alignment when a function has 8-word aligned locals but only
4-word aligned parameters, simply keep the stack frame 8-word aligned at
all times.

As for sizes, the temp def recycler was written before the Ruamoko ISA
was even a pipe dream and thus never expected temp def sizes over 4. At
least now any future adjustments can be done in one place.

My quick and dirty test program works :)

    dvec4 xy = {1d, 2d, 0d, 0.5};
    void printf(string fmt, ...) = #0;
    int main()
    {
	dvec4 u = {3, 4, 3.14};
	dvec4 v = {3, 4, 0, 1};
	dvec4 w = v * xy + u;
	printf ("[%g, %g, %g, %g]\n", w[0], w[1], w[2], w[3]);
	return 0;
    }
2022-02-04 08:46:58 +09:00
Bill Currie
0123e12304 [qfcc] Use locals and params_start to describe stack frame
This is necessary to get statement disassembly working, and likely
debugging in general. locals is the total size of the stack frame and
thus reaches above the function-entry stack pointer, and params_start is
the local space relative start of the parameters. Thus, knowing the
function-entry stack pointer, the bottom of the locals space can be
found by subtracting params_start, and the top of the locals space by
adding (locals - params_start).
2022-01-27 11:37:37 +09:00
Bill Currie
0a5101f88c [qfcc] Specify base register index for local defs
While all base registers can be used for any purpose at any time (this
is why the with instruction has hard-absolute modes: you can never get
permanently lost), qfcc currently uses the convention of register 0 for
globals and register 1 for stack locals (params, locals, function args).
The register used to access a def is stored in the def and that is used
to set the register bits in the instruction opcode.

The def code actually doesn't know anything about any conventions: it
assumes all defs are global for non-temp defs (the function code updates
the defs before emitting code) and the current function provides the
register to use for any temp defs allocated while emitting code.

Seems to work well, but debug is utterly messed up (not surprised, that
will be tricky).
2022-01-21 20:34:43 +09:00
Bill Currie
7bc1396358 [qfcc] Split the function defspace into three spaces
Since Ruamoko now uses the stack for parameters and locals, parameters
need to come after locals in the address space (instead of before, as in
v6 progs). Thus use separate spaces for parameters and locals regardless
of the target, then stitch them together appropriately for the target.
The third space is used for allocating stack space for arguments to
called functions. It us not used for v6 progs, and comes before locals
in Ruamoko progs.

Other than the return value, and optimization (ice, not implemented)
calls in Ruamoko look like they'll work.
2022-01-21 10:20:02 +09:00
Bill Currie
b8b47aaf54 [qfcc] Give finish_function the finishing blow
It's been twitching for over ten years, time to put it out of its
misery.
2022-01-21 00:23:40 +09:00
Bill Currie
2df64384c1 [gamecode] Clean up string_t and pointer_t
They are both gone, and pr_pointer_t is now pr_ptr_t (pointer may be a
little clearer than ptr, but ptr is consistent with things like intptr,
and keeps the type name short).
2022-01-18 12:11:14 +09:00
Bill Currie
4111d44dcc [gamecode] Move progs auxiliary headers into a subdirectory
Just another step along the road of tidying up the QF include directory
(and desirable for generated data).
2022-01-09 00:26:52 +09:00
Bill Currie
22c67fc268 [qfcc] Use flow analysis for dealloc check
I decided that the check for whether control reaches the end of the
function without performing some necessary action (eg, invoking
[super dealoc] in a derived -dealoc) is conceptually the return
statement using a pseudo operand and the necessary action defining that
pseudo operand and thus is the same as checking for uninitialised
variables. Thus, add a pseudo operand type and use one to represent the
invocation of [super alloc], with a special function to call when the
"used" pseudo operand is "uninitialised".

While I currently don't know what else pseudo operands could be used
for, the system should be flexible enough to add any check.

Fixes #24
2021-12-25 17:04:26 +09:00
Bill Currie
7c24f116b4 [qfcc] Rename tmpaddr to pseudo_addr
I want to use the function's pseudo address that was used for managing
aliased temporary variables for other pseudo operands as well. The new
name seems to better reflect the variable's purpose even without the
other pseudo operands as temporary variables are, effectively, pseudo
operands until they are properly allocated.
2021-12-25 12:21:59 +09:00
Bill Currie
7ed12e2f37 [qfcc] Fix static function declarations
I'm surprised it took this long for static not working to cause a
problem.
2021-09-24 19:49:55 +09:00
Bill Currie
8479cad8a8 [qfcc] Record alias-free type in function_t
This eases type unaliasing on functions a little.

Still more to to go, but this fixes a really hair-pulling bug: linux's
heap randomiser was making the typedef test fail randomly whenever
typedef.qfo was compiled.
2020-03-28 15:10:14 +09:00