summaryrefslogtreecommitdiff
path: root/bcc/label.c
blob: 64800d4c23770e665a99a02b343b2169b882b708 (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
/* label.c - label handling routines for bcc */

/* Copyright (C) 1992 Bruce Evans */

#include "bcc.h"
#include "condcode.h"
#include "gencode.h"
#include "label.h"
#include "output.h"
#include "sc.h"
#include "scan.h"
#include "sizes.h"
#include "type.h"

#ifdef I8088
# define outlbranch() outop3str( "b")
# define outsbranch() outop2str( "j")
#endif
#ifdef MC6809
# define outlbranch() outop3str( "LB")
# define outsbranch() outop2str( "B")
#endif

#define MAXVISLAB 32

struct labdatstruct
{
    label_no labnum;		/* 0 if not active */
    offset_T lablc;		/* location counter for branch or label */
    char *labpatch;		/* buffer ptr for branch, NULL for label */
    ccode_t labcond;		/* condition code for branch */
};

#ifdef I8088
PRIVATE char lcondnames[][2] =	/* names of long condition codes */
{
    { 'e', 'q', }, { 'n', 'e', }, { 'r', ' ', }, { 'r', 'n', },
    { 'l', 't', }, { 'g', 'e', }, { 'l', 'e', }, { 'g', 't', },
    { 'l', 'o', }, { 'h', 'i', }, { 'l', 'o', }, { 'h', 'i' },
};
PRIVATE char scondnames[][2] =	/* names of short condition codes */
{
    { 'e', ' ', }, { 'n', 'e', }, { 'm', 'p', }, { 'n', 0, },
    { 'l', ' ', }, { 'g', 'e', }, { 'l', 'e', }, { 'g', ' ', },
    { 'b', ' ', }, { 'a', 'e', }, { 'b', 'e', }, { 'a', ' ', }, 
};
#endif

#ifdef MC6809
PRIVATE char condnames[][2] =	/* names of condition codes */
{
    { 'E', 'Q', }, { 'N', 'E', }, { 'R', 'A', }, { 'R', 'N', },
    { 'L', 'T', }, { 'G', 'E', }, { 'L', 'E', }, { 'G', 'T', },
    { 'L', 'O', }, { 'H', 'S', }, { 'L', 'S', }, { 'H', 'I', },
};
#endif

PRIVATE label_no lasthighlab = 0xFFFF+1;	/* temp & temp init so labels fixed */
				/* lint */
PRIVATE label_no lastlab;	/* bss init to 0 */
PRIVATE offset_T lc;		/* bss init to 0 */

PRIVATE struct labdatstruct vislab[MAXVISLAB];	/* bss, all labnum's init 0 */
PRIVATE smalin_t nextvislab;	/* bss init to NULL */
PRIVATE struct symstruct *namedfirst;	/* bss init to NULL */
PRIVATE struct symstruct *namedlast;	/* bss init to NULL */

FORWARD void addlabel P((ccode_pt cond, label_no label, char *patch));
FORWARD struct labdatstruct *findlabel P((label_no label));

/* add label to circular list */

PRIVATE void addlabel(cond, label, patch)
ccode_pt cond;
label_no label;
char *patch;
{
    register struct labdatstruct *labptr;

    labptr = &vislab[(int)nextvislab];
    labptr->labcond = cond;
    labptr->labnum = label;
    labptr->lablc = lc;
    labptr->labpatch = patch;
    if (++nextvislab == MAXVISLAB)
	nextvislab = 0;
}

/* bump location counter */

PUBLIC void bumplc()
{
    ++lc;
}

/* bump location counter by 2 */

PUBLIC void bumplc2()
{
    lc += 2;
}

/* bump location counter by 3 */

PUBLIC void bumplc3()
{
    lc += 3;
}

/* clear out labels in function */

PUBLIC void clearfunclabels()
{
    register struct symstruct *symptr;
    register struct symstruct *tmp;

    for (symptr = namedfirst; symptr != NULL;)
    {
	if (symptr->indcount == 2)
	    error("undefined label");
	symptr->indcount = 0;
	tmp = symptr;
	symptr = (struct symstruct *) symptr->type;
	tmp->type = NULL;
    }
    namedlast = namedfirst = NULL;
}

/* clear out labels no longer in buffer */

PUBLIC void clearlabels(patchbuf, patchtop)
char *patchbuf;
char *patchtop;
{
    register struct labdatstruct *labptr;
    struct labdatstruct *labtop;
    register char *labpatch;

    for (labptr = &vislab[0], labtop = &vislab[MAXVISLAB];
	 labptr < labtop; ++labptr)
	if ((labpatch = labptr->labpatch) >= patchbuf && labpatch < patchtop)
	    labptr->labnum = 0;
}

/* clear out labels in switch statement */

PUBLIC void clearswitchlabels()
{
    register struct symstruct *symptr;

    for (symptr = namedfirst; symptr != NULL;
	 symptr = (struct symstruct *) symptr->type)
	if (symptr->indcount == 3)
	{
	    equlab(symptr->offset.offlabel, lowsp);
	    symptr->indcount = 4;
	}
}

/* return location counter */

PUBLIC uoffset_T getlc()
{
    return (uoffset_T) lc;
}

/* define location of label and backpatch references to it */

PUBLIC void deflabel(label)
label_no label;
{
    char *cnameptr;
    struct labdatstruct *labmin;
    struct labdatstruct *labmax;
    struct labdatstruct *labmid;
    struct labdatstruct *labptrsave;
    offset_T nlonger;

    outnlabel(label);
    {
	register struct labdatstruct *labptr;
	register char *labpatch;

	labmin = &vislab[0];
	labmax = &vislab[MAXVISLAB];
	labptr = labmid = &vislab[(int)nextvislab];
	if (!watchlc)
	    do
	    {
		if (labptr == labmin)
		    labptr = &vislab[MAXVISLAB];
		--labptr;
		if (labptr->labnum == label)
		{
		    if ((labpatch = labptr->labpatch) != NULL &&
			isshortbranch(lc - labptr->lablc))
		    {
#ifdef I8088 /* patch "bcc(c) to j(c)(c)( ) */
			*labpatch = 'j';
			*(labpatch + 1) =
			    *(cnameptr = scondnames[(int)labptr->labcond]);
#endif
#ifdef MC6809
# ifdef NEW_MC6809 /* patch JMP\t> or LBCC\t to BCC \t */
			*labpatch = 'B';
			*(labpatch + 4) = '\t';	/* redundant unless JMP */
			*(labpatch + 1) =
			    *(cnameptr = condnames[labptr->labcond]);
# else
			if (labptr->labcond == RA)
			    strncpy(labpatch, "BRA\t\t", 5);
			else
			    *labpatch = '\t';
			goto over;
# endif
#endif
			*(labpatch + 2) = *(cnameptr + 1);
			*(labpatch + 3) = ' ';
#ifdef MC6809
# ifndef NEW_MC6809 /* patch JMP\t> or LBCC\t to BCC \t */
		over: ;		/* temp regression test kludge */
# endif
#endif
			nlonger = jcclonger;
			if (labptr->labcond == RA)
			    nlonger = jmplonger;
			lc -= nlonger;
			labptrsave = labptr;
			while (++labptr != labmid)
			    if (labptr == labmax)
				labptr = &vislab[-1];
			    else
				labptr->lablc -= nlonger;
			labptr = labptrsave;
		    }
		}
	    }
	    while (labptr != labmid);
    }
    addlabel((ccode_pt) 0, label, (char *) NULL);
}

PRIVATE struct labdatstruct *findlabel(label)
label_no label;
{
    register struct labdatstruct *labptr;
    struct labdatstruct *labtop;

    for (labptr = &vislab[0], labtop = &vislab[MAXVISLAB];
	 labptr < labtop; ++labptr)
	if (labptr->labnum == label)
	{
	    if (labptr->labpatch != 0)
		break;
	    return labptr;
	}
    return (struct labdatstruct *) NULL;
}

/* reserve a new label, from top down to temp avoid renumbering low labels */

PUBLIC label_no gethighlabel()
{
    return --lasthighlab;
}

/* reserve a new label */

PUBLIC label_no getlabel()
{
    return ++lastlab;
}

/* jump to label */

PUBLIC void jump(label)
label_no label;
{
    lbranch(RA, label);
}

/* long branch on condition to label */

PUBLIC void lbranch(cond, label)
ccode_pt cond;
label_no label;
{
#ifdef I8088
    char *cnameptr;

#endif
    struct labdatstruct *labptr;
    char *oldoutptr;

    if ((ccode_t) cond == RN)
	return;
    if ((labptr = findlabel(label)) != NULL &&
	isshortbranch(lc - labptr->lablc + 2))
    {
	sbranch(cond, label);
	return;
    }
    oldoutptr = outbufptr;
    if (cond == RA)
	outjumpstring();
    else
    {
	outlbranch();
#ifdef I8088
	outbyte(*(cnameptr = lcondnames[(int) cond]));
	outbyte(*(cnameptr + 1));
	if ((ccode_t) cond == LS || (ccode_t) cond == HS)
	    outbyte('s');	/* "blos" or "bhis" */
	else
	    outbyte(' ');
	outtab();
	bumplc2();
#ifdef I80386
	if (i386_32)
	    bumplc();
#endif
#endif
#ifdef MC6809
	outcond(cond);
	bumplc();
#endif
    }
    outlabel(label);
    outnl();
    if (labptr == NULL && oldoutptr < outbufptr)	/* no wrap-around */
	addlabel(cond, label, oldoutptr);
}

/* look up the name gsname in label space, install it if new */

PUBLIC struct symstruct *namedlabel()
{
    struct symstruct *symptr;

    gs2name[1] = 0xFF;
    if ((symptr = findlorg(gs2name + 1)) == NULL)
    {
	symptr = addglb(gs2name + 1, vtype);
	symptr->flags = LABELLED;
    }
    if (symptr->indcount < 2)
    {
	symptr->indcount = 2;
	symptr->offset.offlabel = gethighlabel();
	if (namedfirst == NULL)
	    namedfirst = symptr;
	else
	    namedlast->type = (struct typestruct *) symptr;
	namedlast = symptr;
	symptr->type = NULL;
    }
    return symptr;
}

#ifdef MC6809

/* print condition code name */

PUBLIC void outcond(cond)
ccode_pt cond;
{
    char *cnameptr;

    outbyte(*(cnameptr = condnames[(ccode_t) cond]));
    outbyte(*(cnameptr + 1));
    outtab();
}

#endif

/* print label */

PUBLIC void outlabel(label)
label_no label;
{
    outbyte(LABELSTARTCHAR);
    outhexdigs((uoffset_T) label);
}

/* print label and newline */

PUBLIC void outnlabel(label)
label_no label;
{
    outlabel(label);
#ifdef LABELENDCHAR
    outnbyte(LABELENDCHAR);
#else
    outnl();
#endif
}

/* short branch on condition to label */

PUBLIC void sbranch(cond, label)
ccode_pt cond;
label_no label;
{
#ifdef I8088
    char *cnameptr;

    if ((ccode_t) cond != RN)
    {
	outsbranch();
	outbyte(*(cnameptr = scondnames[(int) cond]));
	outbyte(*(cnameptr + 1));
	outtab();
	outlabel(label);
	outnl();
    }
#endif
#ifdef MC6809
    outsbranch();
    outcond(cond);
    outlabel(label);
    outnl();
#endif
}

/* reverse bump location counter */

PUBLIC void unbumplc()
{
    --lc;
}