[go: up one dir, main page]

File: label.c

package info (click to toggle)
cook 2.10-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,856 kB
  • ctags: 3,842
  • sloc: ansic: 47,714; sh: 12,150; makefile: 4,317; yacc: 3,104; awk: 154
file content (166 lines) | stat: -rw-r--r-- 3,761 bytes parent folder | download | duplicates (5)
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
/*
 *	cook - file construction tool
 *	Copyright (C) 1997 Peter Miller;
 *	All rights reserved.
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it 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.
 *
 *	This program is 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 this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * MANIFEST: functions to manipulate labels
 */

#include <mem.h>
#include <opcode/label.h>
#include <trace.h>


/*
 * NAME
 *	opcode_label_new
 *
 * SYNOPSIS
 *	opcode_label_ty *opcode_label_new(void);
 *
 * DESCRIPTION
 *	The opcode_label_new function is used to allocate a new label
 *	instance in dynamic memory, and initialize it in preparation for
 *	use.
 *
 * CAVEAT
 *	Use opcode_label_delete when you are done with it.
 */

opcode_label_ty *
opcode_label_new()
{
	opcode_label_ty	*lp;

	trace(("opcode_label_new()\n{\n"/*}*/));
	lp = mem_alloc(sizeof(opcode_label_ty));
	lp->pc = (size_t)(-1);
	lp->npending = 0;
	lp->npending_max = 0;
	lp->pending = 0;
	trace(("return %08lX;\n", (long)lp));
	trace((/*{*/"}\n"));
	return lp;
}


/*
 * NAME
 *	opcode_label_delete
 *
 * SYNOPSIS
 *	void opcode_label_delete(opcode_label_ty *);
 *
 * DESCRIPTION
 *	The opcode_label_delete function is used to release the
 *	resources held by an opcode structure.
 */

void
opcode_label_delete(lp)
	opcode_label_ty	*lp;
{
	trace(("opcode_label_delete(lp = %08lX)\n{\n"/*}*/, (long)lp));
	if (lp->pending)
		mem_free(lp->pending);
	lp->pc = (size_t)(-1);
	lp->npending = 0;
	lp->npending_max = 0;
	lp->pending = 0;
	mem_free(lp);
	trace((/*{*/"}\n"));
}


/*
 * NAME
 *	opcode_label_define
 *
 * SYNOPSIS
 *	void opcode_label_define(opcode_label_ty *, size_t);
 *
 * DESCRIPTION
 *	The opcode_label_define function is used to specify the location
 *	of a label.  Outstanding references will be resolved, future
 *	references will be set accordingly.
 *
 * CAVEAT
 *	Should be called exactly once per label.
 */

void
opcode_label_define(lp, where)
	opcode_label_ty	*lp;
	size_t		where;
{
	size_t		j;

	trace(("opcode_label_define(lp = %08lX)\n{\n"/*}*/, (long)lp));
	assert(lp->pc == (size_t)(-1));
	lp->pc = where;
	trace(("pc = %ld\n", (long)lp->pc));
	for (j = 0; j < lp->npending; ++j)
		*lp->pending[j] = lp->pc;
	if (lp->pending)
		mem_free(lp->pending);
	lp->npending = 0;
	lp->npending_max = 0;
	lp->pending = 0;
	trace((/*{*/"}\n"));
}


/*
 * NAME
 *	opcode_label_refer
 *
 * SYNOPSIS
 *	void opcode_label_refer(void);
 *
 * DESCRIPTION
 *	The opcode_label_refer function is used to refer to a label.  If
 *	the label is not yet defined, references will eb accumulated and
 *	resolved after the definition.
 *
 * CAVEAT
 *	The opcodes in which the labels are used are expected to remain
 *	in existence until the label is defined.
 */

void
opcode_label_refer(lp, where)
	opcode_label_ty	*lp;
	size_t		*where;
{
	trace(("opcode_label_refer(lp = %08lX)\n{\n"/*}*/, (long)lp));
	*where = (size_t)(-1);
	if (lp->pc == (size_t)(-1))
	{
		if (lp->npending >= lp->npending_max)
		{
			size_t		nbytes;

			lp->npending_max = lp->npending_max * 2 + 4;
			nbytes = lp->npending_max * sizeof(lp->pending[0]);
			lp->pending = mem_change_size(lp->pending, nbytes);
		}
		lp->pending[lp->npending++] = where;
	}
	else
		*where = lp->pc;
	trace((/*{*/"}\n"));
}