xref: /netbsd/usr.sbin/gspa/gspa/gsp_act.c (revision bf9ec67e)
1 /*	$NetBSD: gsp_act.c,v 1.4 2001/06/13 10:46:06 wiz Exp $	*/
2 /*
3  * GSP assembler - semantic actions
4  *
5  * Copyright (c) 1993 Paul Mackerras.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Paul Mackerras.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: gsp_act.c,v 1.4 2001/06/13 10:46:06 wiz Exp $");
37 #endif
38 
39 #include <stdlib.h>
40 #include <string.h>
41 #include "gsp_ass.h"
42 
43 void
44 free_operands(operand l)
45 {
46 	operand op, oq;
47 
48 	for( op = l; op != NULL; op = oq ){
49 		oq = op->next;
50 		if( op->type == EXPR ||
51 		    (op->type == EA && op->mode >= M_INDEX) )
52 			free_expr(op->op_u.value);
53 		free(op);
54 	}
55 }
56 
57 operand
58 add_operand(operand first, operand last)
59 {
60 	operand p;
61 
62 	for( p = first; p->next != NULL; p = p->next )
63 		;
64 	p->next = last;
65 	return first;
66 }
67 
68 operand
69 reg_op(int reg)
70 {
71 	operand o;
72 
73 /*	printf("reg_op reg=%d sign=%d\n", reg, sign);	*/
74 	new(o);
75 	o->type = REG;
76 	o->reg_no = reg;
77 	o->next = NULL;
78 	return o;
79 }
80 
81 operand
82 expr_op(expr val)
83 {
84 	operand o;
85 
86 /*	printf("immed len=%d\n", len);	*/
87 	new(o);
88 	o->type = EXPR;
89 	o->op_u.value = val;
90 	o->next = NULL;
91 	return o;
92 }
93 
94 operand
95 string_op(char *str)
96 {
97 	operand o;
98 
99 /*	printf("string_op str=%s\n", str);	*/
100 	new(o);
101 	o->type = STR_OPN;
102 	o->op_u.string = str;
103 	o->next = NULL;
104 	return o;
105 }
106 
107 operand
108 abs_adr(expr adr)
109 {
110 	operand o;
111 
112 /*	printf("abs_adr len=%d\n", len);	*/
113 	new(o);
114 	o->type = EA;
115 	o->mode = M_ABSOLUTE;
116 	o->op_u.value = adr;
117 	o->next = NULL;
118 	return o;
119 }
120 
121 operand
122 reg_ind(int reg, int mode)
123 {
124 	operand o;
125 
126 /*	printf("reg_adr r1=%d r2=%d mode=%d\n", r1, r2, mode);	*/
127 	new(o);
128 	o->type = EA;
129 	o->mode = mode;
130 	o->reg_no = reg;
131 	o->next = NULL;
132 	return o;
133 }
134 
135 operand
136 reg_indxy(int reg, char *xy)
137 {
138 	ucasify(xy);
139 	if( strcmp(xy, ".XY") != 0 )
140 		perr("Register format must be .XY");
141 	return reg_ind(reg, M_INDXY);
142 }
143 
144 operand
145 reg_index(int reg, expr disp)
146 {
147 	operand o;
148 
149 	o = reg_ind(reg, M_INDEX);
150 	o->op_u.value = disp;
151 	return o;
152 }
153 
154 expr
155 id_expr(char *id)
156 {
157 	expr x;
158 
159 /*	printf("id_expr id=%s\n", id);	*/
160 	new(x);
161 	x->e_op = SYM;
162 	x->e_sym = lookup(id, TRUE);
163 	return x;
164 }
165 
166 expr
167 num_expr(int val)
168 {
169 	expr x;
170 
171 /*	printf("num_expr val=%d\n", val);	*/
172 	new(x);
173 	x->e_op = CONST;
174 	x->e_val = val;
175 	return x;
176 }
177 
178 expr
179 here_expr()
180 {
181 	expr x;
182 
183 /*	printf("here_expr()\n");	*/
184 	new(x);
185 	x->e_op = '.';
186 	return x;
187 }
188 
189 expr
190 bexpr(int op, expr l, expr r)
191 {
192 	expr x;
193 
194 /*	printf("bexpr op=%d\n", op);	*/
195 	new(x);
196 	x->e_op = op;
197 	x->e_left = l;
198 	x->e_right = r;
199 	return x;
200 }
201 
202 void
203 free_expr(expr x)
204 {
205 	if( x->e_op != SYM && x->e_op != CONST && x->e_op != '.' ){
206 		free_expr(x->e_left);
207 		if( x->e_right != NULL )
208 			free_expr(x->e_right);
209 	}
210 	free(x);
211 }
212