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
|
/*
* bin86/as/genobj.c
*
* Copyright (C) 1992 Bruce Evans
*/
/* genobj.c - object code generation routines for assembler */
#include "const.h"
#include "type.h"
#include "address.h"
#include "file.h"
#include "globvar.h"
#define OBJBUFSIZE 512 /* size of object code output buffer */
#define isge2byteoffset(offset) ((offset) >= 0x100)
#define isge4byteoffset(offset) ((offset) >= 0x10000L)
PRIVATE char hid_absbuf[OBJ_MAX_ABS_LEN]; /* absolute object code buf */
PRIVATE char *absbuf; /* start */
PRIVATE char *absbufend; /* last location + 1 */
PRIVATE char *absbufptr; /* current location */
PRIVATE struct sym_s **arrext; /* array of external symbol ptrs */
PRIVATE char hid_objbuf[OBJBUFSIZE]; /* object code buffer */
PRIVATE unsigned numext; /* number of external symbols */
PRIVATE char *objbuf; /* start */
PRIVATE char *objbufend; /* last location + 1 */
PRIVATE char *objbufptr; /* current location */
PRIVATE unsigned char relsize; /* current relocation size, 0 init */
/* local to genobjadr, but here */
/* because of static re-init bug */
PRIVATE offset_t rmbcount; /* accumulator for repeated RMB's */
FORWARD void flushabs P((void));
FORWARD void flushrmb P((void));
FORWARD void genobjadr P((struct address_s *adrptr, int size));
FORWARD void putobj1 P((opcode_pt c));
FORWARD void putobj4 P((u32_t offset));
FORWARD void putobjoffset P((offset_t offset, count_t size));
FORWARD void putobjword P((unsigned word));
FORWARD void writeobj P((char *buf, unsigned count));
/* accumulate RMB requests into 1 (so + and - requests cancel) */
PUBLIC void accumulate_rmb(offset)
offset_t offset;
{
if (objectc)
{
flushabs();
rmbcount += offset;
}
}
/* flush absolute object code buffer to object code buffer if necessary */
PRIVATE void flushabs()
{
if (absbufptr > absbuf)
{
putobj1((absbufptr - absbuf) | OBJ_ABS);
{
register char *bufptr;
bufptr = absbuf;
do
putobj1(*bufptr);
while (++bufptr < absbufptr);
absbufptr = absbuf;
}
}
}
/* flush object code buffer if necessary */
PUBLIC void flushobj()
{
int ntowrite;
if ((ntowrite = objbufptr - objbuf) > 0)
{
if (write(objfil, objbuf, ntowrite) != ntowrite)
{
error(OBJOUT);
listline();
finishup();
}
objbufptr = objbuf;
}
}
/* flush RMB count if necessary */
PRIVATE void flushrmb()
{
count_t size;
if (rmbcount != 0)
{
#if SIZEOF_OFFSET_T > 2
if (isge4byteoffset(rmbcount))
{
putobj1(OBJ_SKIP_4);
size = 4;
}
else
#endif
if (isge2byteoffset(rmbcount))
{
putobj1(OBJ_SKIP_2);
size = 2;
}
else
{
putobj1(OBJ_SKIP_1);
size = 1;
}
putobjoffset(rmbcount, size);
rmbcount = 0;
}
}
/* generate object code for current line */
/*
any address parameter is (regrettably) in lastexp
any immediate parameter is (corectly) in immadr
*/
PUBLIC void genobj()
{
struct address_s *adrptr;
char *bufptr;
unsigned char remaining;
if (objectc && mcount != 0)
{
if (popflags)
{
if (fcflag)
{
bufptr = databuf.fcbuf;
remaining = mcount;
do
putabs(*bufptr++);
while (--remaining != 0);
}
if (fdflag)
{
adrptr = databuf.fdbuf;
remaining = mcount;
do
genobjadr(adrptr++, 2);
while ((remaining -= 2) != 0);
}
#if SIZEOF_OFFSET_T > 2
if (fqflag)
{
adrptr = databuf.fqbuf;
remaining = mcount;
do
genobjadr(adrptr++, 4);
while ((remaining -= 4) != 0);
}
#endif
}
else
{
remaining = mcount - 1; /* count opcode immediately */
#ifdef I80386
if (aprefix != 0)
{
putabs(aprefix);
--remaining;
}
if (oprefix != 0)
{
putabs(oprefix);
--remaining;
}
if (sprefix != 0)
{
putabs(sprefix);
--remaining;
}
#endif
if (page != 0)
{
putabs(page);
--remaining;
}
putabs(opcode);
if (remaining != 0)
{
if (postb != 0)
{
putabs(postb);
--remaining;
}
#ifdef I80386
if (sib != NO_SIB)
{
putabs(sib);
--remaining;
}
#endif
if (remaining != 0)
genobjadr(&lastexp, remaining);
}
}
#ifdef I80386
if (immcount != 0)
genobjadr(&immadr, immcount);
#endif
}
}
/* generate object code for current address */
PRIVATE void genobjadr(adrptr, size)
struct address_s *adrptr;
smallcount_t size;
{
unsigned char byte;
unsigned symnum;
if (!(adrptr->data & RELBIT))
{
/* absolute address */
char buf[sizeof(offset_t)];
#if SIZEOF_OFFSET_T > 2
u4cn(buf, adrptr->offset, size);
#else
u2cn(buf, adrptr->offset, size);
#endif
putabs(buf[0]);
if (size > 1)
putabs(buf[1]);
if (size > 2)
{
putabs(buf[2]);
putabs(buf[3]);
}
}
else
{
/* relocatable address */
if (size != relsize)
/* set reloc size index |00|0000xx| */
putobj((relsize = size) == 4 ? 0x03 : relsize);
if (!(adrptr->data & IMPBIT))
{
/* offset relocation (known offset) */
putobj((adrptr->data & SEGM) | OBJ_OFFSET_REL | pcrflag);
putobjoffset(adrptr->offset, size);
}
else
{
/* symbol relocation (imported symbol + offset) */
{
register struct sym_s **copyptr;
for (copyptr = arrext, symnum = 0;
symnum < numext && *copyptr++ != adrptr->sym; ++symnum)
;
}
byte = OBJ_SYMBOL_REL;
if (isge2byteoffset(symnum))
byte = OBJ_SYMBOL_REL | OBJ_S_MASK;
#if SIZEOF_OFFSET_T > 2
if (isge4byteoffset(adrptr->offset))
{
byte |= 0x03; /* 4 byte offset */
size = 4;
}
else
#endif
if (isge2byteoffset(adrptr->offset))
{
byte |= 0x02; /* 2 byte offset */
size = 2;
}
else if (adrptr->offset != 0)
{
byte |= 0x01; /* 1 byte offset */
size = 1;
}
else
size = 0;
putobj(byte | pcrflag);
if (isge2byteoffset(symnum))
putobjword(symnum);
else
putobj1(symnum);
if (adrptr->offset != 0)
putobjoffset(adrptr->offset, size);
}
}
}
/* initialise private variables */
PUBLIC void initobj()
{
absbufend = (absbufptr = absbuf = hid_absbuf) + sizeof hid_absbuf;
objbufend = (objbufptr = objbuf = hid_objbuf) + sizeof hid_objbuf;
}
/*
write header to object file
also build array of imported/exported symbols
*/
PUBLIC void objheader()
{
static char module_header[] =
{
#ifdef I80386
0xA3, 0x86,
1, 0,
(char) (0xA3 + 0x86 + 1 + 0),
#endif
#ifdef MC6809
'S', '1', /* 2 byte magic number */
0, 1, /* 2 byte number of modules in file */
'S' + '1' + 0 + 1, /* 1 byte checksum */
#endif
};
static char seg_max_sizes[] =
{
0x55, /* all segments have maximum size 2^16 */
0x55, /* this is encoded by 0b01 4 times per byte */
0x55, /* other codes are 0b00 = max size 2^8 */
0x55, /* 0b10 = max size 2^24, 0b11 = max 2^32 */
};
unsigned char byte;
register struct sym_s **copyptr;
struct sym_s **copytop;
struct sym_s **hashptr;
struct lc_s *lcp;
char module_name[FILNAMLEN + 1];
char *nameptr;
unsigned offset;
unsigned segsizebytes;
unsigned size;
unsigned char sizebits;
unsigned strsiz; /* size of object string table */
unsigned symosiz; /* size of object symbol table */
register struct sym_s *symptr;
u32_t textlength;
if ((objectc = objectg) == 0)
return;
writeobj(module_header, sizeof module_header);
/* calculate number of imported/exported symbols */
/* and lengths of symbol and string tables */
/* build array of imported/exported symbols */
symosiz = 0;
if (truefilename == NULL)
truefilename = filnamptr;
nameptr = strrchr(truefilename, DIRCHAR);
strcpy(module_name, nameptr != NULL ? nameptr + 1 : truefilename);
if ((nameptr = strrchr(module_name, '.')) != NULL)
*nameptr = 0;
strsiz = strlen(module_name) + 1;
align(heapptr);
for (hashptr = spt, arrext = copyptr = (struct sym_s **) heapptr;
hashptr < spt_top;)
if ((symptr = *hashptr++) != NULL)
do
{
if ((symptr->type & EXPBIT || symptr->data & IMPBIT) ||
!globals_only_in_obj && symptr->name[0] != '.' &&
!(symptr->type & (MNREGBIT | MACBIT | VARBIT)))
{
if (copyptr >= (struct sym_s **) heapend)
{
heapptr = (char *) copyptr;
fatalerror(OBJSYMOV);
}
*copyptr++ = symptr;
strsiz += symptr->length + 1;
#if SIZEOF_OFFSET_T > 2
if (isge4byteoffset(symptr->value_reg_or_op.value))
size = 4 + 4;
/* 4 is size of offset into string table and flags */
/* 2nd 4 is for 4 byte offset */
else
#endif
if (isge2byteoffset(symptr->value_reg_or_op.value))
size = 4 + 2;
else if (symptr->value_reg_or_op.value != 0)
size = 4 + 1;
else
size = 4;
symosiz += size;
++numext;
}
}
while ((symptr = symptr->next) != NULL);
heapptr = (char *) (copytop = copyptr);
/* calculate length of text, and number of seg size bytes in header */
textlength = segsizebytes = 0;
lcp = lctab;
do
if (lcp->lc != 0)
{
textlength += lcp->lc; /* assuming text starts at 0 */
#if SIZEOF_OFFSET_T > 2
if (isge4byteoffset(lcp->lc))
segsizebytes += 4;
else
#endif
segsizebytes += 2; /* use 2 byte size if possible */
}
while (++lcp < lctabtop);
/*
offset to text = length of header since only 1 module
header consists of:
module header sizeof module_header
offset to start of text 4
length of text 4
length of string area 2
class 1
revision 1
seg max sizes sizeof seg_max_sizes
seg size descriptors 4
seg sizes segsizebytes
symbol count 2
symbol offsets and types symosiz
strings strsiz
*/
/* offset to start of text */
putobj4((u32_t) (sizeof module_header + 4 + 4 + 2 + 1 + 1 +
sizeof seg_max_sizes + 4 + segsizebytes + 2 +
symosiz) + strsiz);
/* length of text */
putobj4((u32_t) textlength);
/* length of string area */
putobjword(strsiz);
/* class and revision */
putobj1(0);
putobj1(0);
/* segment max sizes (constant) */
writeobj(seg_max_sizes, sizeof seg_max_sizes);
/* segment size descriptors */
/* produce only 0 and 2 byte sizes */
lcp = lctabtop;
byte = 0;
sizebits = OBJ_SEGSZ_TWO << 6;
do
{
--lcp;
if (lcp->lc != 0)
{
byte |= sizebits;
#if SIZEOF_OFFSET_T > 2
if (isge4byteoffset(lcp->lc))
byte |= sizebits >> 1; /* XXX - convert size 2 to size 4 */
#endif
}
if ((sizebits >>= 2) == 0)
{
putobj1(byte);
sizebits = OBJ_SEGSZ_TWO << 6;
}
}
while (lcp > lctab);
/* segment sizes */
do /* lcp starts at lctab */
if (lcp->lc != 0)
{
#if SIZEOF_OFFSET_T > 2
if (isge4byteoffset(lcp->lc))
putobj4(lcp->lc);
else
#endif
putobjword((unsigned) lcp->lc);
}
while (++lcp < lctabtop);
/* symbol count */
putobjword(numext);
/* symbol offsets and types */
offset = strlen(module_name) + 1; /* 1st symbol begins after name */
for (copyptr = arrext; copyptr < copytop;)
{
putobjword(offset);
symptr = *copyptr++;
byte = symptr->type & OBJ_N_MASK;
#if SIZEOF_OFFSET_T > 2
if (isge4byteoffset(symptr->value_reg_or_op.value))
{
byte |= OBJ_SZ_FOUR;
size = 4;
}
else
#endif
if (isge2byteoffset(symptr->value_reg_or_op.value))
{
byte |= OBJ_SZ_TWO;
size = 2;
}
else if (symptr->value_reg_or_op.value != 0)
{
byte |= OBJ_SZ_ONE;
size = 1;
}
else
size = 0;
if ((symptr->type & (COMMBIT | REDBIT)) == (COMMBIT | REDBIT))
{
byte |= OBJ_SA_MASK;
symptr->data &= ~OBJ_I_MASK;
}
putobjword((byte << 0x8) |
(symptr->type & OBJ_E_MASK) | /* |E|0000000| */
((symptr->data & (OBJ_I_MASK | OBJ_A_MASK | OBJ_SEGM_MASK)) ^
/* |0|I|0|A|SEGM| */
RELBIT)); /* RELBIT by negative logic */
if ((symptr->type & (COMMBIT | REDBIT)) == (COMMBIT | REDBIT))
symptr->data |= OBJ_I_MASK;
if (size != 0)
putobjoffset(symptr->value_reg_or_op.value, size);
offset += symptr->length + 1;
}
/* strings */
writeobj(module_name, strlen(module_name));
putobj1(0);
for (copyptr = arrext; copyptr < copytop;)
{
symptr = *copyptr++;
writeobj(symptr->name, symptr->length);
putobj1(0);
}
putobj1(OBJ_SET_SEG | 0); /* default segment 0, |0010|SEGM| */
}
/* write trailer to object file */
PUBLIC void objtrailer()
{
if (objectc)
{
putobj(0); /* end of object file */
flushobj();
}
}
/* write char to absolute object code buffer, flush if necessary */
PUBLIC void putabs(c)
opcode_pt c;
{
if (objectc)
{
if (rmbcount != 0)
flushrmb();
if (absbufptr >= absbufend)
flushabs();
*absbufptr++ = c;
}
}
/* write char to object code buffer, flush if necessary */
PUBLIC void putobj(c)
opcode_pt c;
{
if (objectc)
{
flushabs();
flushrmb();
putobj1(c);
}
}
/* write char to object code buffer assuming nothing in absolute & rmb bufs */
PRIVATE void putobj1(c)
opcode_pt c;
{
if (objbufptr >= objbufend)
flushobj();
*objbufptr++ = c;
}
/* write 32 bit offset to object code buffer assuming ... */
PRIVATE void putobj4(offset)
u32_t offset;
{
char buf[sizeof offset];
u4c4(buf, offset);
writeobj(buf, sizeof buf);
}
/* write sized offset to object code buffer assuming ... */
PRIVATE void putobjoffset(offset, size)
offset_t offset;
count_t size;
{
char buf[sizeof offset];
#if SIZEOF_OFFSET_T > 2
u4cn(buf, offset, size);
#else
u2cn(buf, offset, size);
#endif
putobj1(buf[0]);
if (size > 1)
putobj1(buf[1]);
if (size > 2)
{
putobj1(buf[2]);
putobj1(buf[3]);
}
}
/* write word to object code buffer assuming ... */
PRIVATE void putobjword(word)
unsigned word;
{
char buf[sizeof word];
u2c2(buf, word);
putobj1(buf[0]);
putobj1(buf[1]);
}
/* write several bytes to object code buffer assuming ... */
PRIVATE void writeobj(buf, count)
char *buf;
unsigned count;
{
do
putobj1(*buf++);
while (--count);
}
|