forked from fte-team/fteqw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
q3asm2.c
1233 lines (1044 loc) · 25.6 KB
/
q3asm2.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
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of the fteqw tools.
FTEQW and the Quake III Arena source code are free software; you can redistribute them
and/or modify them under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
FTEQW and the Quake III Arena source code are distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
/*
Copyright (C) 2007 David Walton
GPL.
This file is the core of an assembler/linker to generate q3-compatable qvm files. It is based on the version in the Q3 source code, but rewritten from the ground up.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "../engine/qclib/hash.h"
//from qfiles.h
#define VM_MAGIC 0x12721444
struct vmheader {
int vmMagic;
int instructionCount;
int codeOffset;
int codeLength;
int dataOffset; // data then lit are here
int dataLength;
int litLength; // ( dataLength - litLength ) should be byteswapped on load
int bssLength; // zero filled memory appended to datalength
};
//end of qfiles.h
typedef enum {
OP_UNDEF,
OP_IGNORE,
OP_BREAK,
OP_ENTER,
OP_LEAVE,
OP_CALL,
OP_PUSH,
OP_POP,
OP_CONST,
OP_LOCAL,
OP_JUMP,
//-------------------
OP_EQ,
OP_NE,
OP_LTI,
OP_LEI,
OP_GTI,
OP_GEI,
OP_LTU,
OP_LEU,
OP_GTU,
OP_GEU,
OP_EQF,
OP_NEF,
OP_LTF,
OP_LEF,
OP_GTF,
OP_GEF,
//-------------------
OP_LOAD1,
OP_LOAD2,
OP_LOAD4,
OP_STORE1,
OP_STORE2,
OP_STORE4, // *(stack[top-1]) = stack[yop
OP_ARG,
OP_BLOCK_COPY,
//-------------------
OP_SEX8,
OP_SEX16,
OP_NEGI,
OP_ADD,
OP_SUB,
OP_DIVI,
OP_DIVU,
OP_MODI,
OP_MODU,
OP_MULI,
OP_MULU,
OP_BAND,
OP_BOR,
OP_BXOR,
OP_BCOM,
OP_LSH,
OP_RSHI,
OP_RSHU,
OP_NEGF,
OP_ADDF,
OP_SUBF,
OP_DIVF,
OP_MULF,
OP_CVIF,
OP_CVFI
} opcode_t;
typedef struct {
char *name;
int opcode;
} sourceOps_t;
sourceOps_t sourceOps[] = {
#include "opstrings.h"
};
enum segs {
SEG_CODE,
SEG_DATA,
SEG_LIT,
SEG_BSS,
SEG_MAX,
SEG_UNKNOWN
};
#define SYMBOLBUCKETS 4096
#define LOCALSYMBOLBUCKETS 1024
#define USEDATBLOCK 64
#define STACKSIZE 0x10000
#ifdef WIN32
#define snprintf _snprintf
#endif
struct usedat {
struct usedat *next;
int count;
char seg[USEDATBLOCK];
int offset[USEDATBLOCK];
};
struct symbol {
struct symbol *next;
int inseg; //this is where the actual value is defined
int atoffset;
struct usedat usedat; //this is where it is used
//to help with cpu cache, the first is embeded
bucket_t bucket; //for the hash tables
char name[1]; //a common C hack
};
struct segment {
void *data;
unsigned int base;
unsigned int length;
unsigned int available; //before it needs a realloc
int isdummy; //set if there's no real data assosiated with this segment
};
struct assembler {
struct segment segs[SEG_MAX];
struct segment *curseg;
struct symbol *symbols;
hashtable_t symboltable;
bucket_t symboltablebuckets[SYMBOLBUCKETS];
hashtable_t localsymboltable;
bucket_t localsymboltablebuckets[LOCALSYMBOLBUCKETS];
struct symbol *lastsym; //so we can move symbols that lcc put into the wrong place.
int numinstructions;
int errorcount;
int funclocals;
int funcargs;
int funcargoffset;
int verbose;
};
struct workload {
char output[256];
int numsrcfiles;
char **srcfilelist;
int verbose;
};
void EmitData(struct segment *seg, void *data, int bytes)
{
unsigned char *d, *s=data;
if (seg->isdummy)
{
seg->length += bytes;
return;
}
if (seg->length + bytes > seg->available)
{
unsigned int newlen = (seg->length + bytes + 0x10000) & ~0xffff;
void *newseg;
newseg = realloc(seg->data, newlen);
if (!newseg)
{
printf("out of memory\n");
seg->length += bytes;
seg->isdummy = 1;
return;
}
memset((char*)newseg+seg->available, 0, newlen - seg->available);
seg->data = newseg;
seg->available = newlen;
}
d = (unsigned char*)seg->data + seg->length;
seg->length += bytes;
while(bytes-- > 0)
*d++ = *s++;
}
void EmitByte(struct segment *seg, unsigned char data)
{
EmitData(seg, &data, 1);
}
void SkipData(struct segment *seg, int bytes)
{
seg->length += bytes;
}
void AlignSegment(struct segment *seg, int alignment)
{
//alignment must always be power of 2
alignment--;
seg->length = (seg->length + alignment) & ~(alignment);
}
void AssembleError(struct assembler *w, char *message, ...)
{
va_list va;
va_start(va, message);
vprintf(message, va);
va_end(va);
w->errorcount++;
}
void SegmentHack(struct assembler *w, int seg)
{
//like id, I don't see any way around this
//plus compatability. :/
if (w->curseg != &w->segs[seg])
{
w->curseg = &w->segs[seg];
if (w->lastsym)
{
// if (*w->lastsym->name != '$')
// printf("symbol %s segment-switch hack\n", w->lastsym->name);
w->lastsym->inseg = seg;
w->lastsym->atoffset = w->curseg->length;
}
else
printf("segment-switch hack\n");
}
}
void DoSegmentHack(struct assembler *w, int bytes)
{
if (bytes == 4)
{
SegmentHack(w, SEG_DATA);
}
else if (bytes == 1)
{
SegmentHack(w, SEG_LIT);
}
else
AssembleError(w, "%ibit variables are not supported\n", bytes*8);
}
int ParseToken(char *buffer, int bufferlen, char **input)
{
unsigned char *in = (unsigned char*)*input;
*buffer = 0;
while (*in <= ' ')
{
if (!*in++)
return 0;
}
if (*in == ';') //';' is taken as a comment
return 0;
do
{
if (!--bufferlen)
{
printf("name too long\n");
break;
}
*buffer++ = *in++;
} while (*in > ' ');
*buffer = 0;
*input = (char*)in;
return 1;
}
void AddReference(struct usedat *s, int segnum, int segofs)
{
if (s->count == USEDATBLOCK)
{ //we're getting a lot of references for this var
if (!s->next)
{
s->next = malloc(sizeof(*s));
if (!s->next)
{
printf("out of memory\n");
// AssembleError(w, "out of memory\n");
return;
}
s->next->next = NULL;
s->next->count = 0;
}
AddReference(s->next, segnum, segofs);
return;
}
s->seg[s->count] = segnum;
s->offset[s->count] = segofs;
s->count++;
}
struct symbol *GetSymbol(struct assembler *w, char *name)
{
struct symbol *s;
hashtable_t *t;
if (*name == '$')
t = &w->localsymboltable;
else
t = &w->symboltable;
s = Hash_Get(t, name);
if (!s)
{
s = malloc(sizeof(*s) + strlen(name));
if (!s)
{
AssembleError(w, "out of memory\n");
return NULL;
}
memset(s, 0, sizeof(*s));
s->next = w->symbols;
w->symbols = s;
strcpy(s->name, name);
Hash_Add(t, s->name, s, &s->bucket);
s->inseg = SEG_UNKNOWN;
s->atoffset = 0;
}
return s;
}
void DefineSymbol(struct assembler *w, char *symname, int segnum, int segofs)
{
struct symbol *s;
hashtable_t *t;
if (*symname == '$')
t = &w->localsymboltable;
else
t = &w->symboltable;
if (!*symname) //bail out
*(int*)NULL = -3;
s = Hash_Get(t, symname);
if (s)
{
if (s->inseg != SEG_UNKNOWN)
AssembleError(w, "symbol %s is already defined!\n", symname);
}
else
{
s = malloc(sizeof(*s) + strlen(symname));
if (!s)
{
printf ("out of memory\n");
w->errorcount++;
return;
}
memset(s, 0, sizeof(*s));
s->next = w->symbols;
w->symbols = s;
strcpy(s->name, symname);
Hash_Add(t, s->name, s, &s->bucket);
}
w->lastsym = s;
s->inseg = segnum;
s->atoffset = segofs;
}
int CalculateExpression(struct assembler *w, char *line)
{
char *o;
char valstr[64];
int val;
int segnum = w->curseg - w->segs;
int segoffs = w->curseg->length;
int isnum;
struct symbol *s;
//this is expressed as CONST+NAME-NAME
//many instructions need to add an additional base before emitting it
//so set the references to the place that we expect to be
val = 0;
while (*line)
{
o = valstr;
if (*line == '-' || *line == '+')
{
*o++ = *line++;
}
while (*(unsigned char*)line > ' ')
{
if (*line == '+' || *line == '-')
break;
*o++ = *line++;
}
*o = '\0';
if (*valstr == '-')
isnum = 1;
else if (*valstr == '+')
isnum = (valstr[1] >= '0' && valstr[1] <= '9');
else
isnum = (valstr[0] >= '0' && valstr[0] <= '9');
if (isnum)
val += atoi(valstr);
else
{
if (*valstr == '-' || *valstr == '+')
s = GetSymbol(w, valstr+1);
else
s = GetSymbol(w, valstr);
if (s) //yeah, doesn't make sense to have two symbols in one expression
AddReference(&s->usedat, segnum, segoffs); //but we do it anyway
}
}
return val;
}
void AssembleLine(struct assembler *w, char *line)
{
int i;
int opcode;
char instruction[64];
if (!ParseToken(instruction, sizeof(instruction), &line))
return; //nothing on this line
//try and get our special instructions first
switch (instruction[0])
{
case 'a':
if (!strcmp(instruction+1, "lign"))
{ //align
//theoretically, this should never make a difference. if it does, the segment hack stuff screwed something up.
if (ParseToken(instruction, sizeof(instruction), &line))
AlignSegment(w->curseg, atoi(instruction));
else
AssembleError(w, "align without argument\n");
return;
}
if (!strcmp(instruction+1, "ddress"))
{ //address
// w->lastsym = NULL;
if (ParseToken(instruction, sizeof(instruction), &line))
{
int expression;
SegmentHack(w, SEG_DATA);
expression = CalculateExpression(w, instruction);
EmitData(w->curseg, &expression, 4);
}
return;
}
break;
case 'b':
if (!strcmp(instruction+1, "ss"))
{ //bss
w->curseg = &w->segs[SEG_BSS];
return;
}
if (!strcmp(instruction+1, "yte"))
{ //byte
unsigned int value = 0;
unsigned int bytes = 0;
if (ParseToken(instruction, sizeof(instruction), &line))
bytes = atoi(instruction);
else
AssembleError(w, "byte without count\n");
if (ParseToken(instruction, sizeof(instruction), &line))
value = atoi(instruction);
else
AssembleError(w, "byte without value\n");
if (bytes == 4)
{
SegmentHack(w, SEG_DATA);
EmitData(w->curseg, &value, 4);
}
else if (bytes == 1)
{
SegmentHack(w, SEG_LIT);
EmitByte(w->curseg, value);
}
else
AssembleError(w, "%ibit variables are not supported\n", bytes*8);
return;
}
break;
case 'c':
if (!strcmp(instruction+1, "ode"))
{ //code
w->curseg = &w->segs[SEG_CODE];
return;
}
break;
case 'd':
if (!strcmp(instruction+1, "ata"))
{ //data
w->curseg = &w->segs[SEG_DATA];
return;
}
break;
case 'e':
if (!strcmp(instruction+1, "qu"))
{ //equ
char value[32];
if (ParseToken(instruction, sizeof(instruction), &line))
{
if (ParseToken(value, sizeof(value), &line))
DefineSymbol(w, instruction, w->curseg - w->segs, atoi(value));
else
AssembleError(w, "equ without value\n");
}
else
AssembleError(w, "equ without name\n");
return;
}
if (!strcmp(instruction+1, "xport"))
{ //export (ignored)
return;
}
if (!strcmp(instruction+1, "ndproc"))
{ //endproc
int locals = 0, args = 0;
if (ParseToken(instruction, sizeof(instruction), &line))
locals = atoi(instruction);
if (ParseToken(instruction, sizeof(instruction), &line))
args = atoi(instruction);
EmitByte(&w->segs[SEG_CODE], OP_PUSH); //we must return something
w->numinstructions++;
//disregard the two vars from above (matches q3asm...)
locals = 8 + w->funclocals + w->funcargs;
EmitByte(&w->segs[SEG_CODE], OP_LEAVE);
EmitData(&w->segs[SEG_CODE], &locals, 4);
w->numinstructions++;
return;
}
break;
case 'f':
if (!strcmp(instruction+1, "ile"))
{ //file (ignored)
return;
}
break;
case 'i':
if (!strcmp(instruction+1, "mport"))
{ //import (ignored)
return;
}
break;
case 'l':
if (!strcmp(instruction+1, "ine"))
{ //line (ignored)
return;
}
if (!strcmp(instruction+1, "it"))
{ //lit
w->curseg = &w->segs[SEG_LIT];
return;
}
break;
case 'p':
if (!strcmp(instruction+1, "roc"))
{ //proc
int v;
if (!ParseToken(instruction, sizeof(instruction), &line))
AssembleError(w, "unnamed function\n");
DefineSymbol(w, instruction, SEG_CODE, w->numinstructions);
if (ParseToken(instruction, sizeof(instruction), &line))
w->funclocals = (atoi(instruction) + 3) & ~3;
else
AssembleError(w, "proc without locals\n");
if (ParseToken(instruction, sizeof(instruction), &line))
w->funcargs = (atoi(instruction) + 3) & ~3;
else
AssembleError(w, "proc without args\n");
v = 8 + w->funclocals + w->funcargs;
if (v > 32767)
AssembleError(w, "function with an aweful lot of args\n");
EmitByte(&w->segs[SEG_CODE], OP_ENTER);
EmitData(&w->segs[SEG_CODE], &v, 4);
w->numinstructions++;
return;
}
if (!strcmp(instruction+1, "op"))
{ //pop
EmitByte(&w->segs[SEG_CODE], OP_POP);
w->numinstructions++;
return;
}
break;
case 's':
if (!strcmp(instruction+1, "kip"))
{ //skip
if (ParseToken(instruction, sizeof(instruction), &line))
SkipData(w->curseg, atoi(instruction));
else
AssembleError(w, "skip without argument\n");
return;
}
break;
case 'L':
if (!strncmp(instruction+1, "ABEL", 4))
{
if (!ParseToken(instruction, sizeof(instruction), &line))
{
printf("label with no name\n");
return;
}
//some evilness here (symbols in the instruction segment are instruction indexes not byte offsets)
if (w->curseg == &w->segs[SEG_CODE])
DefineSymbol(w, instruction, w->curseg - w->segs, w->numinstructions);
else
DefineSymbol(w, instruction, w->curseg - w->segs, w->curseg->length);
return;
}
break;
case 'A':
if (!strncmp(instruction+1, "RG", 2))
{ //ARG*
EmitByte(&w->segs[SEG_CODE], OP_ARG);
w->numinstructions++;
if (8 + w->funcargoffset > 255)
AssembleError(w, "too many arguments in fuction\n");
EmitByte(&w->segs[SEG_CODE], 8 + w->funcargoffset);
w->funcargoffset += 4; //reset by a following CALL instruction
return;
}
if (!strncmp(instruction+1, "DDRF", 4))
{ //ADDRF*
int exp = 0;
w->curseg = &w->segs[SEG_CODE]; //this differs, but not for lcc
EmitByte(&w->segs[SEG_CODE], OP_LOCAL);
if (ParseToken(instruction, sizeof(instruction), &line))
exp = CalculateExpression(w, instruction);
exp += 16 + w->funcargs + w->funclocals;
EmitData(&w->segs[SEG_CODE], &exp, 4);
w->numinstructions++;
return;
}
if (!strncmp(instruction+1, "DDRL", 4))
{ //ADDRL
int exp = 0;
w->curseg = &w->segs[SEG_CODE]; //this differs, but not for lcc
EmitByte(&w->segs[SEG_CODE], OP_LOCAL);
if (ParseToken(instruction, sizeof(instruction), &line))
exp = CalculateExpression(w, instruction);
exp += 8 + w->funcargs;
EmitData(&w->segs[SEG_CODE], &exp, 4);
w->numinstructions++;
return;
}
break;
case 'C':
if (!strncmp(instruction+1, "ALL", 3))
{ //CALL*
EmitByte(&w->segs[SEG_CODE], OP_CALL);
w->numinstructions++;
w->funcargoffset = 0;
return;
}
break;
case 'R':
if (!strncmp(instruction+1, "ET", 2))
{
int v = 8 + w->funclocals + w->funcargs;
EmitByte(&w->segs[SEG_CODE], OP_LEAVE);
EmitData(&w->segs[SEG_CODE], &v, 4);
w->numinstructions++;
return;
}
break;
default:
break;
}
//okay, we don't know what it is yet, try generic instructions
for (i = 0; i < sizeof(sourceOps) / sizeof(sourceOps[0]); i++)
{
if (*(unsigned int*)sourceOps[i].name == *(unsigned int*)instruction && !strcmp(sourceOps[i].name+4, instruction+4))
{
opcode = sourceOps[i].opcode;
if (opcode == OP_IGNORE)
return;
if (opcode == OP_SEX8)
{ //sex is special, apparently
if (ParseToken(instruction, sizeof(instruction), &line))
{
if (*instruction == '2')
opcode = OP_SEX16;
else if (*instruction != '1')
AssembleError(w, "bad sign extension\n");
}
}
if (opcode == OP_CVIF || opcode == OP_CVFI)
line = ""; //so we don't fall afoul looking for aguments (float/int conversions are always 4 anyway)
if (ParseToken(instruction, sizeof(instruction), &line))
{
int expression;
w->curseg = &w->segs[SEG_CODE]; //this differs, but not for lcc
EmitByte(&w->segs[SEG_CODE], opcode);
expression = CalculateExpression(w, instruction);
if (opcode == OP_BLOCK_COPY) //string initialisations are block copys too (this could still be wrong, but it matches)
expression = (expression+3)&~3;
EmitData(&w->segs[SEG_CODE], &expression, 4);
}
else
EmitByte(&w->segs[SEG_CODE], opcode);
w->numinstructions++;
return;
}
}
AssembleError(w, "Cannot handle %s\n", instruction);
}
void AssembleFile(struct assembler *w, int fnum, char *filename)
{
char linebuffer[1024];
FILE *f;
if (w->verbose)
{
if (fnum)
printf("file: %i %s\n", fnum, filename);
else
printf("file: %s\n", filename);
}
w->curseg = &w->segs[SEG_CODE];
f = fopen(filename, "rt");
if (!f)
{
snprintf(linebuffer, sizeof(linebuffer)-1, "%s.asm", filename);
f = fopen(linebuffer, "rt");
}
if (f)
{
while(fgets(linebuffer, sizeof(linebuffer), f))
AssembleLine(w, linebuffer);
fclose(f);
}
else
{
printf("couldn't open %s\n", filename);
w->errorcount++;
}
//reset the local symbol hash, so we don't find conflicting vars
memset(w->localsymboltablebuckets, 0, sizeof(w->localsymboltablebuckets));
}
int FixupSymbolReferencesOne(struct assembler *w, struct symbol *s, struct usedat *u)
{
int i;
int val;
unsigned int temp;
unsigned char *ptr;
val = w->segs[s->inseg].base + s->atoffset;
for (i = 0; i < u->count; i++)
{
ptr = (unsigned char*)w->segs[(int)u->seg[i]].data;
ptr += u->offset[i];
temp = (unsigned int)(ptr[0] | (ptr[1]<<8) | (ptr[2]<<16) | (ptr[3]<<24));
*(int*)&temp += val;
ptr[0] = (temp&0x000000ff)>>0;
ptr[1] = (temp&0x0000ff00)>>8;
ptr[2] = (temp&0x00ff0000)>>16;
ptr[3] = (temp&0xff000000)>>24;
}
if (u->next)
i += FixupSymbolReferencesOne(w, s, u->next);
return i;
}
void FixupSymbolReferences(struct assembler *w)
{ //this is our 'second pass'
struct symbol *s;
int numsyms = 0;
int numsymrefs = 0;
if (w->verbose)
printf("second 'pass'\n");
for (s = w->symbols; s; s = s->next)
{
if (s->inseg == SEG_UNKNOWN)
{
AssembleError(w, "Symbol %s was not defined\n", s->name);
continue;
}
if (w->verbose && !s->usedat.count && *s->name != '$') //don't ever mention the compiler generated 'static' vars
printf("%s was never used\n", s->name);
numsymrefs += FixupSymbolReferencesOne(w, s, &s->usedat);
numsyms++;
}
s = GetSymbol(w, "vmMain");
if (s)
{
if (s->atoffset != 0 || s->inseg != SEG_CODE)
AssembleError(w, "vmMain *MUST* be the first symbol in the qvm\nReorder your files\n");
}
if (w->verbose)
{
printf("Found %i symbols\n", numsyms);
printf("Found %i symbol references\n", numsymrefs);
}
}
void WriteMapFile(struct assembler *w, char *fname)
{
int segnum;
struct symbol *s;
FILE *f;
f = fopen(fname, "wt");
if (!f)
return;
//if we sorted these, we'd have better compatability with id's q3asm
//(their linker only defines variables in the second pass, their first pass is only to get table sizes)
//our symbols are in the order they were referenced, rather than used.
for (segnum = 0; segnum < SEG_MAX; segnum++)
{
for (s = w->symbols; s; s = s->next)
{
if (*s->name == '$') //don't show locals
continue;
if (s->inseg != segnum)
continue;
fprintf(f, "%i %8x %s\n", s->inseg, s->atoffset, s->name);
}
}
fclose(f);
}
void InitialiseAssembler(struct assembler *w)
{
int i;
memset(w, 0, sizeof(*w));
Hash_InitTable(&w->symboltable, SYMBOLBUCKETS, w->symboltablebuckets);
Hash_InitTable(&w->localsymboltable, LOCALSYMBOLBUCKETS, w->localsymboltablebuckets);
w->segs[SEG_BSS].isdummy = 1;
i = 0;
EmitData(&w->segs[SEG_DATA], &i, 4); //so NULL really is NULL
}
void WriteOutput(struct assembler *w, char *outputname)
{
FILE *f;
struct vmheader h;
int i;
for (i = 0; i < SEG_MAX; i++)
{ //align the segments (yes, even the code segment)
w->segs[i].length = (w->segs[i].length + 3) & ~3;
}
//add the stack to the end of the bss. I don't know if q3 even uses this
DefineSymbol(w, "_stackStart", SEG_BSS, w->segs[SEG_BSS].length);
w->segs[SEG_BSS].length += STACKSIZE;
DefineSymbol(w, "_stackEnd", SEG_BSS, w->segs[SEG_BSS].length);
w->segs[SEG_CODE].base = 0;
w->segs[SEG_DATA].base = 0;
w->segs[SEG_LIT].base = w->segs[SEG_DATA].base + w->segs[SEG_DATA].length;
w->segs[SEG_BSS].base = w->segs[SEG_LIT].base + w->segs[SEG_LIT].length;
FixupSymbolReferences(w);
if (w->segs[SEG_CODE].isdummy || w->segs[SEG_DATA].isdummy || w->segs[SEG_LIT].isdummy)
w->errorcount++; //one of the segments failed to allocate mem
printf("code bytes: %i\n", w->segs[SEG_CODE].length);
printf("data bytes: %i\n", w->segs[SEG_DATA].length);
printf(" lit bytes: %i\n", w->segs[SEG_LIT ].length);
printf(" bss bytes: %i\n", w->segs[SEG_BSS ].length);
printf("instruction count: %i\n", w->numinstructions);