-
Notifications
You must be signed in to change notification settings - Fork 0
/
qccmain.c
3470 lines (2974 loc) · 92.5 KB
/
qccmain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef MINIMAL
#define PROGSUSED
#include "qcc.h"
int mkdir(const char *path);
char QCC_copyright[1024];
int QCC_packid;
char QCC_Packname[5][128];
extern QCC_def_t *functemps; //floats/strings/funcs/ents...
extern int optres_test1;
extern int optres_test2;
int writeasm;
static pbool pr_werror;
pbool verbose;
pbool QCC_PR_SimpleGetToken (void);
void QCC_PR_LexWhitespace (void);
void *FS_ReadToMem(char *fname, void *membuf, int *len);
void FS_CloseFromMem(void *mem);
struct qcc_includechunk_s *currentchunk;
unsigned int MAX_REGS;
int MAX_STRINGS;
int MAX_GLOBALS;
int MAX_FIELDS;
int MAX_STATEMENTS;
int MAX_FUNCTIONS;
int MAX_CONSTANTS;
int max_temps;
int *qcc_tempofs;
int tempsstart;
int numtemps;
#define MAXSOURCEFILESLIST 8
char sourcefileslist[MAXSOURCEFILESLIST][1024];
int currentsourcefile;
int numsourcefiles;
void QCC_PR_ResetErrorScope(void);
pbool compressoutput;
pbool newstylesource;
char destfile[1024];
float *qcc_pr_globals;
unsigned int numpr_globals;
char *strings;
int strofs;
QCC_dstatement_t *statements;
int numstatements;
int *statement_linenums;
QCC_dfunction_t *functions;
int numfunctions;
QCC_ddef_t *qcc_globals;
int numglobaldefs;
QCC_ddef_t *fields;
int numfielddefs;
//typedef char PATHSTRING[MAX_DATA_PATH];
PATHSTRING *precache_sounds;
int *precache_sounds_block;
int numsounds;
PATHSTRING *precache_textures;
int *precache_textures_block;
int numtextures;
PATHSTRING *precache_models;
int *precache_models_block;
int nummodels;
PATHSTRING *precache_files;
int *precache_files_block;
int numfiles;
extern int numCompilerConstants;
hashtable_t compconstantstable;
hashtable_t globalstable;
hashtable_t localstable;
hashtable_t floatconstdefstable;
hashtable_t stringconstdefstable;
pbool qccwarningdisabled[WARN_MAX];
qcc_targetformat_t qcc_targetformat;
pbool bodylessfuncs;
QCC_type_t *qcc_typeinfo;
int numtypeinfos;
int maxtypeinfos;
struct {
char *name;
int index;
} warningnames[] =
{
{"Q302", WARN_NOTREFERENCED},
// {"", WARN_NOTREFERENCEDCONST},
// {"", WARN_CONFLICTINGRETURNS},
{"Q105", WARN_TOOFEWPARAMS},
{"Q101", WARN_TOOMANYPARAMS},
// {"", WARN_UNEXPECTEDPUNCT},
{"Q106", WARN_ASSIGNMENTTOCONSTANT},
{"Q203", WARN_MISSINGRETURNVALUE},
{"Q204", WARN_WRONGRETURNTYPE},
{"Q205", WARN_POINTLESSSTATEMENT},
{"Q206", WARN_MISSINGRETURN},
{"Q207", WARN_DUPLICATEDEFINITION},
{"Q100", WARN_PRECOMPILERMESSAGE},
// {"", WARN_STRINGTOOLONG},
// {"", WARN_BADTARGET},
{"Q120", WARN_BADPRAGMA},
// {"", WARN_HANGINGSLASHR},
// {"", WARN_NOTDEFINED},
// {"", WARN_SWITCHTYPEMISMATCH},
// {"", WARN_CONFLICTINGUNIONMEMBER},
// {"", WARN_KEYWORDDISABLED},
// {"", WARN_ENUMFLAGS_NOTINTEGER},
// {"", WARN_ENUMFLAGS_NOTBINARY},
// {"", WARN_CASEINSENSATIVEFRAMEMACRO},
{"Q111", WARN_DUPLICATELABEL},
{"Q201", WARN_ASSIGNMENTINCONDITIONAL},
{"F300", WARN_DEADCODE},
{NULL}
};
int QCC_WarningForName(char *name)
{
int i;
for (i = 0; warningnames[i].name; i++)
{
if (!stricmp(name, warningnames[i].name))
return warningnames[i].index;
}
return -1;
}
optimisations_t optimisations[] =
{
//level 0 = no optimisations
//level 1 = size optimisations
//level 2 = speed optimisations
//level 3 = dodgy optimisations.
//level 4 = experimental...
{&opt_assignments, "t", 1, FLAG_ASDEFAULT, "assignments", "c = a*b is performed in one operation rather than two, and can cause older decompilers to fail."},
{&opt_shortenifnots, "i", 1, FLAG_ASDEFAULT, "shortenifs", "if (!a) was traditionally compiled in two statements. This optimisation does it in one, but can cause some decompilers to get confused."},
{&opt_nonvec_parms, "p", 1, FLAG_ASDEFAULT, "nonvec_parms", "In the original qcc, function parameters were specified as a vector store even for floats. This fixes that."},
{&opt_constant_names, "c", 2, FLAG_KILLSDEBUGGERS, "constant_names", "This optimisation strips out the names of constants (but not strings) from your progs, resulting in smaller files. It makes decompilers leave out names or fabricate numerical ones."},
{&opt_constant_names_strings, "cs", 3, FLAG_KILLSDEBUGGERS, "constant_names_strings", "This optimisation strips out the names of string constants from your progs. However, this can break addons, so don't use it in those cases."},
{&opt_dupconstdefs, "d", 1, FLAG_ASDEFAULT, "dupconstdefs", "This will merge definitions of constants which are the same value. Pay extra attention to assignment to constant warnings."},
{&opt_noduplicatestrings, "s", 1, 0, "noduplicatestrings", "This will compact the string table that is stored in the progs. It will be considerably smaller with this."},
{&opt_locals, "l", 1, FLAG_KILLSDEBUGGERS, "locals", "Strips out local names and definitions. This makes it REALLY hard to decompile"},
{&opt_function_names, "n", 1, FLAG_KILLSDEBUGGERS, "function_names", "This strips out the names of functions which are never called. Doesn't make much of an impact though."},
{&opt_filenames, "f", 1, FLAG_KILLSDEBUGGERS, "filenames", "This strips out the filenames of the progs. This can confuse the really old decompilers, but is nothing to the more recent ones."},
{&opt_unreferenced, "u", 1, FLAG_ASDEFAULT, "unreferenced", "Removes the entries of unreferenced variables. Doesn't make a difference in well maintained code."},
{&opt_overlaptemps, "r", 1, FLAG_ASDEFAULT, "overlaptemps", "Optimises the pr_globals count by overlapping temporaries. In QC, every multiplication, division or operation in general produces a temporary variable. This optimisation prevents excess, and in the case of Hexen2's gamecode, reduces the count by 50k. This is the most important optimisation, ever."},
{&opt_constantarithmatic, "a", 1, FLAG_ASDEFAULT, "constantarithmatic", "5*6 actually emits an operation into the progs. This prevents that happening, effectivly making the compiler see 30"},
{&opt_precache_file, "pf", 2, 0, "precache_file", "Strip out stuff wasted used in function calls and strings to the precache_file builtin (which is actually a stub in quake)."},
{&opt_return_only, "ro", 3, FLAG_KILLSDEBUGGERS, "return_only", "Functions ending in a return statement do not need a done statement at the end of the function. This can confuse some decompilers, making functions appear larger than they were."},
{&opt_compound_jumps, "cj", 3, FLAG_KILLSDEBUGGERS, "compound_jumps", "This optimisation plays an effect mostly with nested if/else statements, instead of jumping to an unconditional jump statement, it'll jump to the final destination instead. This will bewilder decompilers."},
// {&opt_comexprremoval, "cer", 4, 0, "expression_removal", "Eliminate common sub-expressions"}, //this would be too hard...
{&opt_stripfunctions, "sf", 3, 0, "strip_functions", "Strips out the 'defs' of functions that were only ever called directly. This does not affect saved games."},
{&opt_locals_marshalling, "lm", 4, FLAG_KILLSDEBUGGERS, "locals_marshalling", "Store all locals in one section of the pr_globals. Vastly reducing it. This effectivly does the job of overlaptemps. It's been noticed as buggy by a few, however, and the curcumstances where it causes problems are not yet known."},
{&opt_vectorcalls, "vc", 4, FLAG_KILLSDEBUGGERS, "vectorcalls", "Where a function is called with just a vector, this causes the function call to store three floats instead of one vector. This can save a good number of pr_globals where those vectors contain many duplicate coordinates but do not match entirly."},
{NULL}
};
#define defaultkeyword FLAG_HIDDENINGUI|FLAG_ASDEFAULT|FLAG_MIDCOMPILE
#define nondefaultkeyword FLAG_HIDDENINGUI|0|FLAG_MIDCOMPILE
//global to store useage to, flags, codename, human-readable name, help text
compiler_flag_t compiler_flag[] = {
//keywords
{&keyword_asm, defaultkeyword, "asm", "Keyword: asm", "Disables the 'asm' keyword. Use the writeasm flag to see an example of the asm."},
{&keyword_break, defaultkeyword, "break", "Keyword: break", "Disables the 'break' keyword."},
{&keyword_case, defaultkeyword, "case", "Keyword: case", "Disables the 'case' keyword."},
{&keyword_class, defaultkeyword, "class", "Keyword: class", "Disables the 'class' keyword."},
{&keyword_const, defaultkeyword, "const", "Keyword: const", "Disables the 'const' keyword."},
{&keyword_continue, defaultkeyword, "continue", "Keyword: continue", "Disables the 'continue' keyword."},
{&keyword_default, defaultkeyword, "default", "Keyword: default", "Disables the 'default' keyword."},
{&keyword_entity, defaultkeyword, "entity", "Keyword: entity", "Disables the 'entity' keyword."},
{&keyword_enum, defaultkeyword, "enum", "Keyword: enum", "Disables the 'enum' keyword."}, //kinda like in c, but typedef not supported.
{&keyword_enumflags, defaultkeyword, "enumflags", "Keyword: enumflags", "Disables the 'enumflags' keyword."}, //like enum, but doubles instead of adds 1.
{&keyword_extern, defaultkeyword, "extern", "Keyword: extern", "Disables the 'extern' keyword. Use only on functions inside addons."}, //function is external, don't error or warn if the body was not found
{&keyword_float, defaultkeyword, "float", "Keyword: float", "Disables the 'float' keyword. (Disables the float keyword without 'local' preceeding it)"},
{&keyword_for, defaultkeyword, "for", "Keyword: for", "Disables the 'for' keyword. Syntax: for(assignment; while; increment) {codeblock;}"},
{&keyword_goto, defaultkeyword, "goto", "Keyword: goto", "Disables the 'goto' keyword."},
{&keyword_int, defaultkeyword, "int", "Keyword: int", "Disables the 'int' keyword."},
{&keyword_integer, defaultkeyword, "integer", "Keyword: integer", "Disables the 'integer' keyword."},
{&keyword_noref, defaultkeyword, "noref", "Keyword: noref", "Disables the 'noref' keyword."}, //nowhere else references this, don't strip it.
{&keyword_nosave, defaultkeyword, "nosave", "Keyword: nosave", "Disables the 'nosave' keyword."}, //don't write the def to the output.
{&keyword_shared, defaultkeyword, "shared", "Keyword: shared", "Disables the 'shared' keyword."}, //mark global to be copied over when progs changes (part of FTE_MULTIPROGS)
{&keyword_state, nondefaultkeyword,"state", "Keyword: state", "Disables the 'state' keyword."},
{&keyword_string, defaultkeyword, "string", "Keyword: string", "Disables the 'string' keyword."},
{&keyword_struct, defaultkeyword, "struct", "Keyword: struct", "Disables the 'struct' keyword."},
{&keyword_switch, defaultkeyword, "switch", "Keyword: switch", "Disables the 'switch' keyword."},
{&keyword_thinktime, nondefaultkeyword,"thinktime", "Keyword: thinktime", "Disables the 'thinktime' keyword which is used in HexenC"},
{&keyword_typedef, defaultkeyword, "typedef", "Keyword: typedef", "Disables the 'typedef' keyword."}, //fixme
{&keyword_union, defaultkeyword, "union", "Keyword: union", "Disables the 'union' keyword."}, //you surly know what a union is!
{&keyword_var, defaultkeyword, "var", "Keyword: var", "Disables the 'var' keyword."},
{&keyword_vector, defaultkeyword, "vector", "Keyword: vector", "Disables the 'vector' keyword."},
//options
{&keywords_coexist, FLAG_ASDEFAULT, "kce", "Keywords Coexist", "If you want keywords to NOT be disabled when they a variable by the same name is defined, check here."},
{&output_parms, 0, "parms", "Define offset parms", "if PARM0 PARM1 etc should be defined by the compiler. These are useful if you make use of the asm keyword for function calls, or you wish to create your own variable arguments. This is an easy way to break decompilers."}, //controls weather to define PARMx for the parms (note - this can screw over some decompilers)
{&autoprototype, 0, "autoproto", "Automatic Prototyping","Causes compilation to take two passes instead of one. The first pass, only the definitions are read. The second pass actually compiles your code. This means you never have to remember to prototype functions again."}, //so you no longer need to prototype functions and things in advance.
{&writeasm, 0, "wasm", "Dump Assembler", "Writes out a qc.asm which contains all your functions but in assembler. This is a great way to look for bugs in fteqcc, but can also be used to see exactly what your functions turn into, and thus how to optimise statements better."}, //spit out a qc.asm file, containing an assembler dump of the ENTIRE progs. (Doesn't include initialisation of constants)
{&flag_ifstring, FLAG_MIDCOMPILE,"ifstring", "if(string) fix", "Causes if(string) to behave identically to if(string!="") This is most useful with addons of course, but also has adverse effects with FRIK_FILE's fgets, where it becomes impossible to determin the end of the file. In such a case, you can still use asm {IF string 2;RETURN} to detect eof and leave the function."}, //correction for if(string) no-ifstring to get the standard behaviour.
{&flag_iffloat, FLAG_MIDCOMPILE,"iffloat","if(-0.0) fix","Fixes certain floating point logic."},
{&flag_acc, 0, "acc", "Reacc support", "Reacc is a pascall like compiler. It was released before the Quake source was released. This flag has a few effects. It sorts all qc files in the current directory into alphabetical order to compile them. It also allows Reacc global/field distinctions, as well as allows ¦ as EOF. Whilst case insensativity and lax type checking are supported by reacc, they are seperate compiler flags in fteqcc."}, //reacc like behaviour of src files.
{&flag_caseinsensative, 0, "caseinsens", "Case insensativity", "Causes fteqcc to become case insensative whilst compiling names. It's generally not advised to use this as it compiles a little more slowly and provides little benefit. However, it is required for full reacc support."}, //symbols will be matched to an insensative case if the specified case doesn't exist. This should b usable for any mod
{&flag_laxcasts, FLAG_MIDCOMPILE,"lax", "Lax type checks", "Disables many errors (generating warnings instead) when function calls or operations refer to two normally incompatible types. This is required for reacc support, and can also allow certain (evil) mods to compile that were originally written for frikqcc."}, //Allow lax casting. This'll produce loadsa warnings of course. But allows compilation of certain dodgy code.
{&flag_hashonly, FLAG_MIDCOMPILE,"hashonly", "Hash-only constants", "Allows use of only #constant for precompiler constants, allows certain preqcc using mods to compile"},
{&opt_logicops, FLAG_MIDCOMPILE,"lo", "Logic ops", "This changes the behaviour of your code. It generates additional if operations to early-out in if statements. With this flag, the line if (0 && somefunction()) will never call the function. It can thus be considered an optimisation. However, due to the change of behaviour, it is not considered so by fteqcc. Note that due to inprecisions with floats, this flag can cause runaway loop errors within the player walk and run functions (without iffloat also enabled). This code is advised:\nplayer_stand1:\n if (self.velocity_x || self.velocity_y)\nplayer_run\n if (!(self.velocity_x || self.velocity_y))"},
{&flag_msvcstyle, FLAG_MIDCOMPILE,"msvcstyle", "MSVC-style errors", "Generates warning and error messages in a format that msvc understands, to facilitate ide integration."},
{&flag_fasttrackarrays, FLAG_MIDCOMPILE|FLAG_ASDEFAULT,"fastarrays","fast arrays where possible", "Generates extra instructions inside array handling functions to detect engine and use extension opcodes only in supporting engines.\nAdds a global which is set by the engine if the engine supports the extra opcodes. Note that this applies to all arrays or none."},
{&flag_assume_integer, FLAG_MIDCOMPILE,"assumeint", "Assume Integers", "Numerical constants are assumed to be integers, instead of floats."},
{NULL}
};
struct {
qcc_targetformat_t target;
char *name;
} targets[] = {
{QCF_STANDARD, "standard"},
{QCF_STANDARD, "q1"},
{QCF_STANDARD, "quakec"},
{QCF_HEXEN2, "hexen2"},
{QCF_HEXEN2, "h2"},
{QCF_KK7, "kkqwsv"},
{QCF_KK7, "kk7"},
{QCF_KK7, "bigprogs"},
{QCF_KK7, "version7"},
{QCF_KK7, "kkqwsv"},
{QCF_FTE, "fte"},
{QCF_DARKPLACES,"darkplaces"},
{QCF_DARKPLACES,"dp"},
{0, NULL}
};
/*
=================
BspModels
Runs qbsp and light on all of the models with a .bsp extension
=================
*/
int QCC_CheckParm (char *check);
void QCC_BspModels (void)
{
int p;
char *gamedir;
int i;
char *m;
char cmd[1024];
char name[256];
p = QCC_CheckParm ("-bspmodels");
if (!p)
return;
if (p == myargc-1)
QCC_Error (ERR_BADPARMS, "-bspmodels must preceed a game directory");
gamedir = myargv[p+1];
for (i=0 ; i<nummodels ; i++)
{
m = precache_models[i];
if (strcmp(m+strlen(m)-4, ".bsp"))
continue;
strcpy (name, m);
name[strlen(m)-4] = 0;
sprintf (cmd, "qbsp %s/%s ; light -extra %s/%s", gamedir, name, gamedir, name);
system (cmd);
}
}
// CopyString returns an offset from the string heap
int QCC_CopyString (char *str)
{
int old;
char *s;
if (opt_noduplicatestrings)
{
if (!str || !*str)
return 0;
for (s = strings; s < strings+strofs; s++)
if (!strcmp(s, str))
{
optres_noduplicatestrings += strlen(str);
return s-strings;
}
}
old = strofs;
strcpy (strings+strofs, str);
strofs += strlen(str)+1;
return old;
}
int QCC_CopyDupBackString (char *str)
{
int old;
char *s;
for (s = strings+strofs-1; s>strings ; s--)
if (!strcmp(s, str))
return s-strings;
old = strofs;
strcpy (strings+strofs, str);
strofs += strlen(str)+1;
return old;
}
void QCC_PrintStrings (void)
{
int i, l, j;
for (i=0 ; i<strofs ; i += l)
{
l = strlen(strings+i) + 1;
printf ("%5i : ",i);
for (j=0 ; j<l ; j++)
{
if (strings[i+j] == '\n')
{
putchar ('\\');
putchar ('n');
}
else
putchar (strings[i+j]);
}
printf ("\n");
}
}
/*void QCC_PrintFunctions (void)
{
int i,j;
QCC_dfunction_t *d;
for (i=0 ; i<numfunctions ; i++)
{
d = &functions[i];
printf ("%s : %s : %i %i (", strings + d->s_file, strings + d->s_name, d->first_statement, d->parm_start);
for (j=0 ; j<d->numparms ; j++)
printf ("%i ",d->parm_size[j]);
printf (")\n");
}
}*/
void QCC_PrintFields (void)
{
int i;
QCC_ddef_t *d;
for (i=0 ; i<numfielddefs ; i++)
{
d = &fields[i];
printf ("%5i : (%i) %s\n", d->ofs, d->type, strings + d->s_name);
}
}
void QCC_PrintGlobals (void)
{
int i;
QCC_ddef_t *d;
for (i=0 ; i<numglobaldefs ; i++)
{
d = &qcc_globals[i];
printf ("%5i : (%i) %s\n", d->ofs, d->type, strings + d->s_name);
}
}
int encode(int len, int method, char *in, int handle);
int WriteSourceFiles(int h, dprograms_t *progs, pbool sourceaswell)
{
includeddatafile_t *idf;
qcc_cachedsourcefile_t *f;
int num=0;
int ofs;
/*
for (f = qcc_sourcefile; f ; f=f->next)
{
if (f->type == FT_CODE && !sourceaswell)
continue;
SafeWrite(h, f->filename, strlen(f->filename)+1);
i = PRLittleLong(f->size);
SafeWrite(h, &i, sizeof(int));
i = PRLittleLong(encrpytmode);
SafeWrite(h, &i, sizeof(int));
if (encrpytmode)
for (i = 0; i < f->size; i++)
f->file[i] ^= 0xA5;
SafeWrite(h, f->file, f->size);
}*/
for (f = qcc_sourcefile,num=0; f ; f=f->next)
{
if (f->type == FT_CODE && !sourceaswell)
continue;
num++;
}
if (!num)
return 0;
idf = qccHunkAlloc(sizeof(includeddatafile_t)*num);
for (f = qcc_sourcefile,num=0; f ; f=f->next)
{
if (f->type == FT_CODE && !sourceaswell)
continue;
strcpy(idf[num].filename, f->filename);
idf[num].size = f->size;
#ifdef AVAIL_ZLIB
idf[num].compmethod = 2;
#else
idf[num].compmethod = 1;
#endif
idf[num].ofs = SafeSeek(h, 0, SEEK_CUR);
idf[num].compsize = QC_encode(progfuncs, f->size, idf[num].compmethod, f->file, h);
num++;
}
ofs = SafeSeek(h, 0, SEEK_CUR);
SafeWrite(h, &num, sizeof(int));
SafeWrite(h, idf, sizeof(includeddatafile_t)*num);
qcc_sourcefile = NULL;
return ofs;
}
void QCC_InitData (void)
{
static char parmname[12][MAX_PARMS];
static temp_t ret_temp;
int i;
qcc_sourcefile = NULL;
numstatements = 1;
strofs = 1;
numfunctions = 1;
numglobaldefs = 1;
numfielddefs = 1;
memset(&ret_temp, 0, sizeof(ret_temp));
def_ret.ofs = OFS_RETURN;
def_ret.name = "return";
def_ret.temp = &ret_temp;
def_ret.constant = false;
def_ret.type = NULL;
ret_temp.ofs = def_ret.ofs;
ret_temp.scope = NULL;
ret_temp.size = 3;
ret_temp.next = NULL;
for (i=0 ; i<MAX_PARMS ; i++)
{
def_parms[i].temp = NULL;
def_parms[i].type = NULL;
def_parms[i].ofs = OFS_PARM0 + 3*i;
def_parms[i].name = parmname[i];
sprintf(parmname[i], "parm%i", i);
}
}
int WriteBodylessFuncs (int handle)
{
QCC_def_t *d;
int ret=0;
for (d=pr.def_head.next ; d ; d=d->next)
{
if (d->type->type == ev_function && !d->scope)// function parms are ok
{
if (d->initialized != 1 && d->references>0)
{
SafeWrite(handle, d->name, strlen(d->name)+1);
ret++;
}
}
}
return ret;
}
//marshalled locals remaps all the functions to use the range MAX_REGS onwards for the offset to their locals.
//this function remaps all the locals back into the function.
void QCC_UnmarshalLocals(void)
{
QCC_def_t *def;
unsigned int ofs;
unsigned int maxo;
int i;
ofs = numpr_globals;
maxo = ofs;
for (def = pr.def_head.next ; def ; def = def->next)
{
if (def->ofs >= MAX_REGS) //unmap defs.
{
def->ofs = def->ofs + ofs - MAX_REGS;
if (maxo < def->ofs)
maxo = def->ofs;
}
}
for (i = 0; i < numfunctions; i++)
{
if (functions[i].parm_start == MAX_REGS)
functions[i].parm_start = ofs;
}
QCC_RemapOffsets(0, numstatements, MAX_REGS, MAX_REGS + maxo-numpr_globals + 3, ofs);
numpr_globals = maxo+3;
if (numpr_globals > MAX_REGS)
QCC_Error(ERR_TOOMANYGLOBALS, "Too many globals are in use to unmarshal all locals");
if (maxo-ofs)
printf("Total of %i marshalled globals\n", maxo-ofs);
}
CompilerConstant_t *QCC_PR_CheckCompConstDefined(char *def);
pbool QCC_WriteData (int crc)
{
char element[MAX_NAME];
QCC_def_t *def, *comp_x, *comp_y, *comp_z;
QCC_ddef_t *dd;
dprograms_t progs;
int h;
int i, len;
pbool debugtarget = false;
pbool types = false;
int outputsize = 16;
if (numstatements==1 && numfunctions==1 && numglobaldefs==1 && numfielddefs==1)
{
printf("nothing to write\n");
return false;
}
progs.blockscompressed=0;
if (numstatements > MAX_STATEMENTS)
QCC_Error(ERR_TOOMANYSTATEMENTS, "Too many statements - %i\nAdd \"MAX_STATEMENTS\" \"%i\" to qcc.cfg", numstatements, (numstatements+32768)&~32767);
if (strofs > MAX_STRINGS)
QCC_Error(ERR_TOOMANYSTRINGS, "Too many strings - %i\nAdd \"MAX_STRINGS\" \"%i\" to qcc.cfg", strofs, (strofs+32768)&~32767);
QCC_UnmarshalLocals();
switch (qcc_targetformat)
{
case QCF_HEXEN2:
case QCF_STANDARD:
if (bodylessfuncs)
printf("Warning: There are some functions without bodies.\n");
if (numpr_globals > 65530 )
{
printf("Forcing target to FTE32 due to numpr_globals\n");
outputsize = 32;
}
else if (qcc_targetformat == QCF_HEXEN2)
{
printf("Progs execution requires a Hexen2 compatible engine\n");
break;
}
else
{
if (numpr_globals >= 32768) //not much of a different format. Rewrite output to get it working on original executors?
printf("An enhanced executor will be required (FTE/QF/KK)\n");
else
printf("Progs should run on any Quake executor\n");
break;
}
//intentional
qcc_targetformat = QCF_FTE;
case QCF_FTEDEBUG:
case QCF_FTE:
case QCF_DARKPLACES:
if (qcc_targetformat == QCF_FTEDEBUG)
debugtarget = true;
if (numpr_globals > 65530)
{
printf("Using 32 bit target due to numpr_globals\n");
outputsize = 32;
}
if (qcc_targetformat == QCF_DARKPLACES)
compressoutput = 0;
//compression of blocks?
if (compressoutput) progs.blockscompressed |=1; //statements
if (compressoutput) progs.blockscompressed |=2; //defs
if (compressoutput) progs.blockscompressed |=4; //fields
if (compressoutput) progs.blockscompressed |=8; //functions
if (compressoutput) progs.blockscompressed |=16; //strings
if (compressoutput) progs.blockscompressed |=32; //globals
if (compressoutput) progs.blockscompressed |=64; //line numbers
if (compressoutput) progs.blockscompressed |=128; //types
//include a type block?
types = debugtarget;//!!QCC_PR_CheckCompConstDefined("TYPES"); //useful for debugging and saving (maybe, anyway...).
if (verbose)
{
if (qcc_targetformat == QCF_DARKPLACES)
printf("DarkPlaces or FTE will be required\n");
else
printf("An FTE executor will be required\n");
}
break;
case QCF_KK7:
if (bodylessfuncs)
printf("Warning: There are some functions without bodies.\n");
if (numpr_globals > 65530)
printf("Warning: Saving is not supported. Ensure all engine read fields and globals are defined early on.\n");
printf("A KK compatible executor will be required (FTE/KK)\n");
break;
}
//part of how compilation works. This def is always present, and never used.
def = QCC_PR_GetDef(NULL, "end_sys_globals", NULL, false, 0, false);
if (def)
def->references++;
def = QCC_PR_GetDef(NULL, "end_sys_fields", NULL, false, 0, false);
if (def)
def->references++;
for (def = pr.def_head.next ; def ; def = def->next)
{
if (def->type->type == ev_vector || (def->type->type == ev_field && def->type->aux_type->type == ev_vector))
{ //do the references, so we don't get loadsa not referenced VEC_HULL_MINS_x
sprintf(element, "%s_x", def->name);
comp_x = QCC_PR_GetDef(NULL, element, def->scope, false, 0, false);
sprintf(element, "%s_y", def->name);
comp_y = QCC_PR_GetDef(NULL, element, def->scope, false, 0, false);
sprintf(element, "%s_z", def->name);
comp_z = QCC_PR_GetDef(NULL, element, def->scope, false, 0, false);
h = def->references;
if (comp_x && comp_y && comp_z)
{
h += comp_x->references;
h += comp_y->references;
h += comp_z->references;
if (!def->references)
if (!comp_x->references || !comp_y->references || !comp_z->references) //one of these vars is useless...
h=0;
def->references = h;
if (!h)
h = 1;
if (comp_x)
comp_x->references = h;
if (comp_y)
comp_y->references = h;
if (comp_z)
comp_z->references = h;
}
}
if (def->references<=0)
{
if (def->constant)
QCC_PR_Warning(WARN_NOTREFERENCEDCONST, strings + def->s_file, def->s_line, "%s no references", def->name);
else
QCC_PR_Warning(WARN_NOTREFERENCED, strings + def->s_file, def->s_line, "%s no references", def->name);
if (opt_unreferenced && def->type->type != ev_field)
{
optres_unreferenced++;
continue;
}
}
if (def->type->type == ev_function)
{
if (opt_function_names && functions[G_FUNCTION(def->ofs)].first_statement<0)
{
optres_function_names++;
def->name = "";
}
if (!def->timescalled)
{
if (def->references<=1)
QCC_PR_Warning(WARN_DEADCODE, strings + def->s_file, def->s_line, "%s is never directly called or referenced (spawn function or dead code)", def->name);
// else
// QCC_PR_Warning(WARN_DEADCODE, strings + def->s_file, def->s_line, "%s is never directly called", def->name);
}
if (opt_stripfunctions && def->timescalled >= def->references-1) //make sure it's not copied into a different var.
{ //if it ever does self.think then it could be needed for saves.
optres_stripfunctions++; //if it's only ever called explicitly, the engine doesn't need to know.
continue;
}
// df = &functions[numfunctions];
// numfunctions++;
}
else if (def->type->type == ev_field)// && !def->constant)
{
dd = &fields[numfielddefs];
numfielddefs++;
dd->type = def->type->aux_type->type;
dd->s_name = QCC_CopyString (def->name);
dd->ofs = G_INT(def->ofs);
}
else if ((def->scope||def->constant) && (def->type->type != ev_string || opt_constant_names_strings))
{
if (opt_constant_names)
{
if (def->type->type == ev_string)
optres_constant_names_strings += strlen(def->name);
else
optres_constant_names += strlen(def->name);
continue;
}
}
// if (!def->saved && def->type->type != ev_string)
// continue;
dd = &qcc_globals[numglobaldefs];
numglobaldefs++;
if (types)
dd->type = def->type-qcc_typeinfo;
else
dd->type = def->type->type;
#ifdef DEF_SAVEGLOBAL
if ( def->saved && ((!def->initialized || def->type->type == ev_function)
// && def->type->type != ev_function
&& def->type->type != ev_field
&& def->scope == NULL))
{
dd->type |= DEF_SAVEGLOBAL;
}
#endif
if (def->shared)
dd->type |= DEF_SHARED;
if (opt_locals && (def->scope || !strcmp(def->name, "IMMEDIATE")))
{
dd->s_name = 0;
optres_locals += strlen(def->name);
}
else
dd->s_name = QCC_CopyString (def->name);
dd->ofs = def->ofs;
}
for (i = 0; i < numglobaldefs; i++)
{
dd = &qcc_globals[i];
if (!(dd->type & DEF_SAVEGLOBAL)) //only warn about saved ones.
continue;
for (h = 0; h < numglobaldefs; h++)
{
if (i == h)
continue;
if (dd->ofs == qcc_globals[h].ofs)
{
if (dd->type != qcc_globals[h].type)
{
if (dd->type != ev_vector && qcc_globals[h].type != ev_float)
QCC_PR_Warning(0, NULL, 0, "Mismatched union global types (%s and %s)", strings+dd->s_name, strings+qcc_globals[h].s_name);
}
//remove the saveglobal flag on the duplicate globals.
qcc_globals[h].type &= ~DEF_SAVEGLOBAL;
}
}
}
for (i = 1; i < numfielddefs; i++)
{
dd = &fields[i];
if (dd->type == ev_vector) //just ignore vectors.
continue;
for (h = 1; h < numfielddefs; h++)
{
if (i == h)
continue;
if (dd->ofs == fields[h].ofs)
{
if (dd->type != fields[h].type)
{
if (fields[h].type != ev_vector)
{
QCC_PR_Warning(0, NULL, 0, "Mismatched union field types (%s and %s)", strings+dd->s_name, strings+fields[h].s_name);
}
}
}
}
}
if (numglobaldefs > MAX_GLOBALS)
QCC_Error(ERR_TOOMANYGLOBALS, "Too many globals - %i\nAdd \"MAX_GLOBALS\" \"%i\" to qcc.cfg", numglobaldefs, (numglobaldefs+32768)&~32767);
for (i = 0; i < nummodels; i++)
{
if (!precache_models_used[i])
QCC_PR_Warning(WARN_EXTRAPRECACHE, NULL, 0, "Model %s was precached but not directly used", precache_models[i]);
else if (!precache_models_block[i])
QCC_PR_Warning(WARN_NOTPRECACHED, NULL, 0, "Model %s was used but not precached", precache_models[i]);
}
//PrintStrings ();
//PrintFunctions ();
//PrintFields ();
//PrintGlobals ();
strofs = (strofs+3)&~3;
if (verbose)
{
printf ("%6i strofs (of %i)\n", strofs, MAX_STRINGS);
printf ("%6i numstatements (of %i)\n", numstatements, MAX_STATEMENTS);
printf ("%6i numfunctions (of %i)\n", numfunctions, MAX_FUNCTIONS);
printf ("%6i numglobaldefs (of %i)\n", numglobaldefs, MAX_GLOBALS);
printf ("%6i numfielddefs (%i unique) (of %i)\n", numfielddefs, pr.size_fields, MAX_FIELDS);
printf ("%6i numpr_globals (of %i)\n", numpr_globals, MAX_REGS);
}
if (!*destfile)
strcpy(destfile, "progs.dat");
if (verbose)
printf("Writing %s\n", destfile);
h = SafeOpenWrite (destfile, 2*1024*1024);
SafeWrite (h, &progs, sizeof(progs));
SafeWrite (h, "\r\n\r\n", 4);
SafeWrite (h, QCC_copyright, strlen(QCC_copyright)+1);
SafeWrite (h, "\r\n\r\n", 4);
while(SafeSeek (h, 0, SEEK_CUR) & 3)//this is a lame way to do it
{
SafeWrite (h, "\0", 1);
}
progs.ofs_strings = SafeSeek (h, 0, SEEK_CUR);
progs.numstrings = strofs;
if (progs.blockscompressed&16)
{
SafeWrite (h, &len, sizeof(int)); //save for later
len = QC_encode(progfuncs, strofs*sizeof(char), 2, (char *)strings, h); //write
i = SafeSeek (h, 0, SEEK_CUR);
SafeSeek(h, progs.ofs_strings, SEEK_SET);//seek back
len = PRLittleLong(len);
SafeWrite (h, &len, sizeof(int)); //write size.
SafeSeek(h, i, SEEK_SET);
}
else
SafeWrite (h, strings, strofs);
progs.ofs_statements = SafeSeek (h, 0, SEEK_CUR);
progs.numstatements = numstatements;
if (qcc_targetformat == QCF_HEXEN2)
{
for (i=0 ; i<numstatements ; i++)
{
if (statements[i].op >= OP_CALL1 && statements[i].op <= OP_CALL8)
QCC_Error(ERR_BADTARGETSWITCH, "Target switching produced incompatible instructions");
else if (statements[i].op >= OP_CALL1H && statements[i].op <= OP_CALL8H)
statements[i].op = statements[i].op - OP_CALL1H + OP_CALL1;
}
}
for (i=0 ; i<numstatements ; i++)
switch(qcc_targetformat == QCF_KK7?32:outputsize) //KK7 sucks.
{
case 32:
for (i=0 ; i<numstatements ; i++)
{
statements[i].op = PRLittleLong/*PRLittleShort*/(statements[i].op);
statements[i].a = PRLittleLong/*PRLittleShort*/(statements[i].a);
statements[i].b = PRLittleLong/*PRLittleShort*/(statements[i].b);
statements[i].c = PRLittleLong/*PRLittleShort*/(statements[i].c);
}
if (progs.blockscompressed&1)
{
SafeWrite (h, &len, sizeof(int)); //save for later
len = QC_encode(progfuncs, numstatements*sizeof(QCC_dstatement32_t), 2, (char *)statements, h); //write
i = SafeSeek (h, 0, SEEK_CUR);
SafeSeek(h, progs.ofs_statements, SEEK_SET);//seek back
len = PRLittleLong(len);
SafeWrite (h, &len, sizeof(int)); //write size.
SafeSeek(h, i, SEEK_SET);
}
else
SafeWrite (h, statements, numstatements*sizeof(QCC_dstatement32_t));
break;
case 16:
#define statements16 ((QCC_dstatement16_t*) statements)
for (i=0 ; i<numstatements ; i++) //resize as we go - scaling down
{
statements16[i].op = PRLittleShort((unsigned short)statements[i].op);
if (statements[i].a < 0)
statements16[i].a = PRLittleShort((short)statements[i].a);
else
statements16[i].a = (unsigned short)PRLittleShort((unsigned short)statements[i].a);
if (statements[i].b < 0)
statements16[i].b = PRLittleShort((short)statements[i].b);
else
statements16[i].b = (unsigned short)PRLittleShort((unsigned short)statements[i].b);
if (statements[i].c < 0)
statements16[i].c = PRLittleShort((short)statements[i].c);
else
statements16[i].c = (unsigned short)PRLittleShort((unsigned short)statements[i].c);
}
if (progs.blockscompressed&1)
{
SafeWrite (h, &len, sizeof(int)); //save for later
len = QC_encode(progfuncs, numstatements*sizeof(QCC_dstatement16_t), 2, (char *)statements16, h); //write
i = SafeSeek (h, 0, SEEK_CUR);
SafeSeek(h, progs.ofs_statements, SEEK_SET);//seek back
len = PRLittleLong(len);
SafeWrite (h, &len, sizeof(int)); //write size.
SafeSeek(h, i, SEEK_SET);
}
else
SafeWrite (h, statements16, numstatements*sizeof(QCC_dstatement16_t));
break;
default:
Sys_Error("intsize error");
}
progs.ofs_functions = SafeSeek (h, 0, SEEK_CUR);
progs.numfunctions = numfunctions;
for (i=0 ; i<numfunctions ; i++)
{
functions[i].first_statement = PRLittleLong (functions[i].first_statement);
functions[i].parm_start = PRLittleLong (functions[i].parm_start);
functions[i].s_name = PRLittleLong (functions[i].s_name);
functions[i].s_file = PRLittleLong (functions[i].s_file);
functions[i].numparms = PRLittleLong ((functions[i].numparms>MAX_PARMS)?MAX_PARMS:functions[i].numparms);
functions[i].locals = PRLittleLong (functions[i].locals);
}
if (progs.blockscompressed&8)
{
SafeWrite (h, &len, sizeof(int)); //save for later
len = QC_encode(progfuncs, numfunctions*sizeof(QCC_dfunction_t), 2, (char *)functions, h); //write
i = SafeSeek (h, 0, SEEK_CUR);
SafeSeek(h, progs.ofs_functions, SEEK_SET);//seek back
len = PRLittleLong(len);
SafeWrite (h, &len, sizeof(int)); //write size.
SafeSeek(h, i, SEEK_SET);
}
else
SafeWrite (h, functions, numfunctions*sizeof(QCC_dfunction_t));
switch(outputsize)
{
case 32:
progs.ofs_globaldefs = SafeSeek (h, 0, SEEK_CUR);
progs.numglobaldefs = numglobaldefs;
for (i=0 ; i<numglobaldefs ; i++)
{