summaryrefslogtreecommitdiff
path: root/bcc/output.c
blob: bdf64f67fe691d88777e4ac878456a941f0ceed0 (plain)
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
/* output.c - output and error handling for bcc */

/* Copyright (C) 1992 Bruce Evans */

#include "bcc.h"
#include "input.h"
#include "os.h"
#include "sizes.h"
#include "table.h"

#undef EXTERN
#define EXTERN
#include "output.h"

#ifdef XENIX_AS
# define HEXSTARTCHAR '/'
#else
# define HEXSTARTCHAR '$'
#endif
#define OUTBUFSIZE 2048
#define opcodeleadin()		/* outtab() for fussy assemblers */

PRIVATE unsigned errcount;	/* # errors in compilation */
				/* depends on zero init */
PRIVATE char hexdigits[] = "0123456789ABCDEF";
PRIVATE char *outbuf;
EXTERN char *outbufend;		/* end of pair of output buffers */
PRIVATE char *outbufmid;
PRIVATE fd_t output;
PRIVATE fastin_t outstage;	/* depends on zero init */

FORWARD void errorsummary P((void));
FORWARD void errsum1 P((void));
#ifdef MC6809
#ifdef DBNODE
FORWARD void outvaldigs P((uvalue_t num));
#endif
#endif

PUBLIC void bugerror(message)
char *message;
{
    error2error("compiler bug? - ", message);
}

PUBLIC void closeout()
{
    char *saveoutptr;

    if (outstage == 3)
    {
	saveoutptr = outbufptr;
	flushout();		/* buffer from last stage */
	outbufptr = saveoutptr;
    }
    outstage = 0;		/* flush from top to current ptr */
    flushout();
    close(output);
}

/* error handler */

PUBLIC void error(message)
char *message;
{
    error2error(message, "");
}

/* error handler - concatenate 2 messages */

PUBLIC void error2error(message1, message2)
char *message1;
char *message2;
{
    char *warning;

    if (message1[0] == '%' && message1[1] == 'w')
    {
	message1 += 2;
	warning = "warning: ";
    }
    else
    {
	++errcount;
	warning = "error: ";
    }
    if (output != 1)
    {
	char *old_outbuf;
	char *old_outbufptr;
	char *old_outbuftop;
	fd_t old_output;
	fastin_t old_outstage;
	char smallbuf[81];	/* don't use heap - might be full or busy */

	old_outbuf = outbuf;
	old_outbufptr = outbufptr;
	old_outbuftop = outbuftop;
	old_output = output;
	old_outstage = outstage;

	outbufptr = outbuf = &smallbuf[0];
	outbuftop = &smallbuf[sizeof smallbuf];
	output = 1;
	outstage = 0;
	errorloc();
	outstr(warning);
	outstr(message1);
	outstr(message2);
	outnl();
	flushout();

	outbuf = old_outbuf;
	outbufptr = old_outbufptr;
	outbuftop = old_outbuftop;
	output = old_output;
	outstage = old_outstage;
    }
    outstr("fail");
    comment();
    errorloc();
    outstr(warning);
    outstr(message1);
    outstr(message2);
    outnl();
}

/* summarise errors */

PRIVATE void errorsummary()
{
    if (errcount != 0)
    {
	outfail();
	errsum1();
    }
    outnl();
    comment();
    errsum1();
}

PRIVATE void errsum1()
{
    outudec(errcount);
    outnstr(" errors detected");
}

/* fatal error, exit early */

PUBLIC void fatalerror(message)
char *message;
{
    error(message);
    finishup();
}

/* finish up compile */

PUBLIC void finishup()
{
#ifdef BUILTIN_CPP
    if (!orig_cppmode)
#endif
    {
	if (watchlc)
	{
	    cseg();
	    outop0str("if *-.program.start-");
	    outnhex(getlc());
	    outfail();
	    outnstr("phase error");
	    outop0str("endif\n");
	}
#ifdef HOLDSTRINGS
	dumpstrings();
#endif
	dumpglbs();
	errorsummary();
    }
    closein();
    closeout();
    exit(errcount == 0 ? 0 : 1);
}

/* flush output file */

PUBLIC void flushout()
{
    unsigned nbytes;

    switch (outstage)
    {
    case 0:
	nbytes = (unsigned) (outbufptr - outbuf);
	outbufptr = outbuf;
	break;
    case 2:
	nbytes = OUTBUFSIZE;
	outbufptr = outbuf;
	outbuftop = outbufmid;
	outstage = 3;
	break;
    default:
	nbytes = OUTBUFSIZE;
	if (outstage == 1)
	    nbytes = 0;
	outbufptr = outbufmid;
	outbuftop = outbufend;
	outstage = 2;
	break;
    }
    if (nbytes != 0)
    {
#ifdef BUILTIN_CPP
	if (!orig_cppmode)
#endif
	    clearlabels(outbufptr, outbufptr + nbytes);
	if (write(output, outbufptr, nbytes) != nbytes)
	{
	    write(2, "output file error\n", 18);
	    closein();
	    close(output);
	    exit(1);
	}
    }
}

PUBLIC void initout()
{
    static char smallbuf[1];

    outbufend = outbuftop = (outbuf = outbufptr = smallbuf) + sizeof smallbuf;
    output = 1;			/* standard output */
}

PUBLIC void limiterror(message)
char *message;
{
    error2error("compiler limit exceeded - ", message);
    finishup();
}

PUBLIC void openout(oname)
char *oname;
{
    if (output != 1)
	fatalerror("more than one output file");
    if ((output = creat(oname, CREATPERMS)) < 0)
    {
	output = 1;
	fatalerror("cannot open output");
    }
}

/* print character */

PUBLIC void outbyte(c)
int c;
{
#if C_CODE || __AS09__ + __AS386_16__ + __AS386_32__ != 1
    register char *outp;

    outp = outbufptr;
    *outp++ = c;
    outbufptr = outp;
    if (outp >= outbuftop)
	flushout();
#else /* !C_CODE etc */

#if __AS09__
# asm
	TFR	X,D
	LDX	_outbufptr,PC
	STB	,X+
	STX	_outbufptr,PC
	CMPX	_outbuftop,PC
	LBHS	CALL.FLUSHOUT
# endasm
#endif /* __AS09__ */

#if __AS386_16__
# asm
# if !__FIRST_ARG_IN_AX__
	pop	dx
	pop	ax
	dec	sp
	dec	sp
# else
#  if ARGREG != DREG
	xchg	ax,bx
#  endif
# endif
	mov	bx,[_outbufptr]
	mov	[bx],al
	inc	bx
	mov	[_outbufptr],bx
	cmp	bx,[_outbuftop]
	jae	Outbyte.Flush
# if !__FIRST_ARG_IN_AX__
	jmp	dx
# else
	ret
# endif

Outbyte.Flush:
# if !__FIRST_ARG_IN_AX__
	push	dx
# endif
	br	_flushout
# endasm
#endif /* __AS386_16__ */

#if __AS386_32__
# asm
# if !__FIRST_ARG_IN_AX__
	mov	eax,_outbyte.c[esp]
# else
#  if ARGREG != DREG
	xchg	eax,ebx
#  endif
# endif
	mov	ecx,[_outbufptr]
	mov	[ecx],al
	inc	ecx
	mov	[_outbufptr],ecx
	cmp	ecx,[_outbuftop]
	jae	Outbyte.Flush
	ret

Outbyte.Flush:
	br	_flushout
# endasm
#endif /* __AS386_32__ */
#endif /* C_CODE etc */
}

/* print comma */

PUBLIC void outcomma()
{
    outbyte(',');
}

/* print line number in format ("# %u \"%s\"%s", nr, fname, str) */

PUBLIC void outcpplinenumber(nr, fname, str)
unsigned nr;
char *fname;
char *str;
{
    outstr("# ");
    outudec(nr);
    outstr(" \"");
    outstr(fname);
    outstr("\"");
    outnstr(str);
}

/* print unsigned offset, hex format */

PUBLIC void outhex(num)
uoffset_T num;
{
#ifdef HEXSTARTCHAR
    if (num >= 10)
	outbyte(HEXSTARTCHAR);
#endif
    outhexdigs(num);
#ifdef HEXENDCHAR
    if (num >= 10)
	outbyte(HEXENDCHAR);
#endif
}

/* print unsigned offset, hex format with digits only (no hex designator) */

PUBLIC void outhexdigs(num)
register uoffset_T num;
{
    if (num >= 0x10)
    {
	outhexdigs(num / 0x10);
	num %= 0x10;
    }
    outbyte(hexdigits[(int) num]);
}

/* print string terminated by EOL */

PUBLIC void outline(s)
char *s;
{
    register char *outp;
    register char *rs;

    outp = outbufptr;
    rs = s;
#ifdef COEOL
    if (*rs == COEOL)
	++rs;
#endif
    do
    {
	*outp++ = *rs;
	if (outp >= outbuftop)
	{
	    outbufptr = outp;
	    flushout();
	    outp = outbufptr;
	}
    }
    while (*rs++ != EOL);
    outbufptr = outp;
#ifdef COEOL
    if (*rs == COEOL)
	outbyte(COEOL);
#endif
}

/* print minus sign */

PUBLIC void outminus()
{
    outbyte('-');
}

/* print character, then newline */

PUBLIC void outnbyte(byte)
int byte;
{
    outbyte(byte);
    outnl();
}

/* print unsigned offset, hex format, then newline */

PUBLIC void outnhex(num)
uoffset_T num;
{
    outhex(num);
    outnl();
}

/* print newline */

PUBLIC void outnl()
{
    if (watchlc)
    {
	outtab();
	comment();
	outhex(getlc());
    }
    outbyte('\n');
}

/* print opcode and newline, bump lc by 1 */

PUBLIC void outnop1str(s)
char *s;
{
    opcodeleadin();
    outstr(s);
    bumplc();
    outnl();
}

/* print opcode and newline, bump lc by 2 */

PUBLIC void outnop2str(s)
char *s;
{
    opcodeleadin();
    outstr(s);
    bumplc2();
    outnl();
}

/* print string, then newline */

PUBLIC void outnstr(s)
char *s;
{
    outstr(s);
    outnl();
}

/* print opcode */

PUBLIC void outop0str(s)
char *s;
{
    opcodeleadin();
    outstr(s);
}

/* print opcode, bump lc by 1 */

PUBLIC void outop1str(s)
char *s;
{
    opcodeleadin();
    outstr(s);
    bumplc();
}

/* print opcode, bump lc by 2 */

PUBLIC void outop2str(s)
char *s;
{
    opcodeleadin();
    outstr(s);
    bumplc2();
}

/* print opcode, bump lc by 3 */

PUBLIC void outop3str(s)
char *s;
{
    opcodeleadin();
    outstr(s);
    bumplc3();
}

/* print plus sign */

PUBLIC void outplus()
{
    outbyte('+');
}

/* print signed offset, hex format */

PUBLIC void outshex(num)
offset_T num;
{
    if (num >= -(maxoffsetto + 1))
    {
	outminus();
	num = -num;
    }
    outhex((uoffset_T) num);
}

/* print string  */

PUBLIC void outstr(s)
char *s;
{
#if C_CODE || __AS09__ + __AS386_16__ + __AS386_32__ != 1
    register char *outp;
    register char *rs;

    outp = outbufptr;
    rs = s;
    while (*rs)
    {
	*outp++ = *rs++;
	if (outp >= outbuftop)
	{
	    outbufptr = outp;
	    flushout();
	    outp = outbufptr;
	}
    }
    outbufptr = outp;
#else /* !C_CODE etc */

#if __AS09__
# asm
	LEAU	,X
	LDX	_outbuftop,PC
	PSHS	X
	LDX	_outbufptr,PC
	BRA	OUTSTR.NEXT

CALL.FLUSHOUT
	PSHS	U,B
	STX	_outbufptr,PC
	LBSR	_flushout
	LDX	_outbufptr,PC
	LDY	_outbuftop,PC
	PULS	B,U,PC

OUTSTR.LOOP
	STB	,X+
	CMPX	,S
	BLO	OUTSTR.NEXT
	BSR	CALL.FLUSHOUT
	STY	,S
OUTSTR.NEXT
	LDB	,U+
	BNE	OUTSTR.LOOP
	STX	_outbufptr,PC
	LEAS	2,S
# endasm
#endif /* __AS09__ */

#if __AS386_16__
# asm
# if !__CALLER_SAVES__
	mov	dx,di
	mov	cx,si
# endif
# if !__FIRST_ARG_IN_AX__
	pop	ax
	pop	si
	dec	sp
	dec	sp
	push	ax
# else
#  if ARGREG == DREG
	xchg	si,ax
#  else
	mov	si,bx
#  endif
# endif
	mov	di,[_outbufptr]
	mov	bx,[_outbuftop]
	br	OUTSTR.NEXT

CALL.FLUSHOUT:
	push	si
# if !__CALLER_SAVES__
	push	dx
	push	cx
# endif
	push	ax
	mov	[_outbufptr],di
	call	_flushout
	mov	di,[_outbufptr]
	mov	bx,[_outbuftop]
	pop	ax
# if !__CALLER_SAVES__
	pop	cx
	pop	dx
#endif
	pop	si
	ret

OUTSTR.LOOP:
	stosb
	cmp	di,bx
	jb	OUTSTR.NEXT
	call	CALL.FLUSHOUT
OUTSTR.NEXT:
	lodsb
	test	al,al
	jne	OUTSTR.LOOP
	mov	[_outbufptr],di
# if !__CALLER_SAVES__
	mov	si,cx
	mov	di,dx
# endif
# endasm
#endif /* __AS386_16__ */

#if __AS386_32__
# asm
# if !__CALLER_SAVES__
	mov	edx,edi
	push	esi
#  define TEMPS 4
# else
#  define TEMPS 0
# endif
# if !__FIRST_ARG_IN_AX__
	mov	esi,TEMPS+_outstr.s[esp]
# else
#  if ARGREG == DREG
	xchg	esi,eax
#  else
	mov	esi,ebx
#  endif
# endif
	mov	edi,[_outbufptr]
	mov	ecx,[_outbuftop]
	br	OUTSTR.NEXT

CALL.FLUSHOUT:
	push	esi
# if !__CALLER_SAVES__
	push	edx
# endif
	push	eax
	mov	[_outbufptr],edi
	call	_flushout
	mov	edi,[_outbufptr]
	mov	ecx,[_outbuftop]
	pop	eax
# if !__CALLER_SAVES__
	pop	edx
# endif
	pop	esi
	ret

OUTSTR.LOOP:
	stosb
	cmp	edi,ecx
	jb	OUTSTR.NEXT
	call	CALL.FLUSHOUT
OUTSTR.NEXT:
	lodsb
	test	al,al
	jne	OUTSTR.LOOP
	mov	[_outbufptr],edi
# if !__CALLER_SAVES__
	pop	esi
	mov	edi,edx
# endif
# endasm
#endif /* __AS386_32__ */
#endif /* C_CODE etc */
}

/* print tab */

PUBLIC void outtab()
{
    outbyte('\t');
}

/* print unsigned, decimal format */

PUBLIC void outudec(num)
unsigned num;
{
    char str[10 + 1];

    str[sizeof str - 1] = 0;
    outstr(pushudec(str + sizeof str - 1, num));
}

#ifdef MC6809
#ifdef DBNODE

/* print unsigned value, hex format (like outhex except value_t is larger) */

PUBLIC void outuvalue(num)
uvalue_t num;
{
#ifdef HEXSTARTCHAR
    if (num >= 10)
	outbyte(HEXSTARTCHAR);
#endif
    outvaldigs(num);
#ifdef HEXENDCHAR
    if (num >= 10)
	outbyte(HEXENDCHAR);
#endif
}

/* print unsigned value, hex format with digits only (no hex designator) */

PRIVATE void outvaldigs(num)
register uvalue_t num;
{
    if (num >= 0x10)
    {
	outvaldigs(num / 0x10);
	num %= 0x10;
    }
    outbyte(hexdigits[(fastin_t) num]);
}

/* print signed value, hex format (like outshex except value_t is larger) */

PUBLIC void outvalue(num)
register value_t num;
{
    if (num < 0)
    {
	outminus();
	num = -num;
    }
    outuvalue((uoffset_T) num);
}

#endif /* DBNODE */
#endif /* MC6809 */

/* push decimal digits of an unsigned onto a stack of chars */

PUBLIC char *pushudec(s, num)
register char *s;
register unsigned num;
{
    register unsigned reduction;

    while (num >= 10)
    {
	reduction = num / 10;
	*--s = num - 10 * reduction + '0';
	num = reduction;
    }
    *--s = num + '0';
    return s;
}

PUBLIC void setoutbufs()
{
    if (!isatty(output))
    {
	outbufptr = outbuf = ourmalloc(2 * OUTBUFSIZE);
#ifdef TS
ts_s_outputbuf += 2 * OUTBUFSIZE;
#endif
	outbufmid = outbuftop = outbufptr + OUTBUFSIZE;
	outbufend = outbufmid + OUTBUFSIZE;
	outstage = 1;
    }
    if (watchlc)
	outnstr(".program.start:\n");	/* kludge temp label */
}